1.Define a file hosts with the entries below, where : 192.168.1.3 is the IP address of the node manager, and 192.168.1.5 and 192.168.1.6 are simple nodes.
# Hosts
node1 ansible_host=192.168.1.3
node2 ansible_host=192.168.1.5
node3 ansible_host=192.168.1.6
[swarm]
node1
node2
node3
2.On node manager (192.168.1.103) run :
$ docker swarm init 3. Run the docker swarm join command on the rest of nodes, using ansible
$ ansible swarm:’!192.168.1.103′ -m shell -a ‘docker swarm join –token SWMTKN-1-32c9u5o0y7gbp3zg8wlvkacbcz0jcwnfgzyhitn5xk8v3gi7s5-2pyej0kduwv6kmx7ynjjvxnhl 192.168.1.103:2377′
The token WMTKN-1-32c9u5o0y7gbp3zg8wlvkacbcz0jcwnfgzyhitn5xk8v3gi7s5-2pyej0kduwv6kmx7ynjjvxnhl should depends on the return of the command : dockers swarm init.
4.Open 2377 TCP port on your firewall if you have one activated.
$ ufw allow 2377/tcp
5.Create the service :
$ docker service create –name hello-world –replicas 3 -p 80:80 dockercloud/hello-world