How to set up Swarm cluster with Ansible? Based on Docker Engine > 1.12

In a previous post we set up a Swarm cluster and there was not easy, there was a lot of things to set up. Since the version 1.12, Docker Engine has a Swarm built-in orchestration tool. And this is changing everything: no more Consul node, no more TSL certificates to generate.

Clone the git project

Clone my ansible roles from Github:

and go to the project directory:

Configure ansible

Set up the hosts

Create the hosts file and open it:

Add to this file the hosts of your Swarm cluster and create the group swarm with all these hosts:

Ensure that you have root access to the hosts.

Set up the playbook

Create the main.yaml file and open it:

To your swarm group add the roles docker (to install docker engine) and docker-swarm (to configure swarm). You must provide the var docker_swarm_secret shared by the hosts, and the var docker_swarm_manager which is the I.P. address of the host you connect to manage the Swarm cluster:

Set up the host variables

Into the directory host_vars create a file with the name of the swarm manager host, here it is node1:

In this file add the variable docker_swarm_is_main_manager to indicate that our host is the Swarm manager:

Run ansible

Just run the command:

That’s it!

Manage the Swarm

Connect to your Swarm manager (you can connect to your host and manage the Swarm with another user if this user is in the group docker):

Create a service:

Listing the tasks of this service you see that 3 containers are running on different nodes:


Leave a Reply