Ansible is an open-source automation engine that automates software provisioning, configuration management, and application deployment.
Installation
# mkdir ansibleSwarm
# mkdir -p ansibleSwarm/ansible/inventory
# Hosts
manager ansible_host=107.23.74.224 ansible_ssh_user=centos ansible_ssh_private_key_file=/home/rodrigo/Development/innuy/AnsibleSwarm/ansible/private/DockerSwarm.pem
worker1 ansible_host=34.205.4.171 ansible_ssh_user=centos ansible_ssh_private_key_file=/home/rodrigo/Development/innuy/AnsibleSwarm/ansible/private/DockerSwarm.pem
[swarm-managers]manager
swarm-workers
-
manager – logical name of the server
-
ansible_host – IP or DNS name of the server
-
ansible_ssh_user – Username which ansible is going to use to connect to the server using SSH.
-
ansible_ssh_private_key_file – Path of the ssh key of the AWS server.
# mkdir -p ansibleSwarm/ansible/roles
—
– name: Clean yum
become: true
shell: yum clean all
tags: [common]
– name: upgrade all packages
become: true
yum: name=* state=latest
tags: [common]
– name: Disable selinux
selinux: state=disabled
become: true
tags: [common]
– name: install unzip
become: true
yum: name=unzip
tags: [common]
– name: Enable the EPEL repository definitions.
become: true
yum: pkg=epel-release
tags: [common]
– name: Install python setup tools
become: true
yum: name=python-setuptools
tags: [common]
– name: Install Pypi
become: true
easy_install: name=pip
tags: [common]
– name: install git
become: true
Yum:
name: git
state: present
tags: [common]
# mkdir -p ansibleSwarm/ansible/roles/docker/tasks
– name: Install docker-py
become: true
pip:
name: docker-py
extra_args: –ignore-installed
tags: [docker]
– name: Update docker repo
become: true
lineinfile:
dest: /etc/yum.repos.d/docker.repo
create: yes
line: “{{ item }}”
with_items:
– “[dockerrepo]”
– “name=Docker Repository”
– “baseurl=https://yum.dockerproject.org/repo/main/centos/7/”
– “enabled=1”
– “gpgcheck=1”
– “gpgkey=https://yum.dockerproject.org/gpg”
tags: [docker]
– name: Install Docker
become: true
yum: pkg=docker-engine state=present
tags: [docker]
– name: enable sysv dockerd service
become: true
service:
name: docker.service
enabled: yes
tags: [docker]
– name: Start service
become: true
service:
name: docker.service
state: started
tags: [docker]
# mkdir -p ansibleSwarm/ansible/roles/dockermanager/tasks
—
– name: Disabling swarm
become: yes
command: docker swarm leave -f
ignore_errors: yes
tags: [dockermanager]
– name: initialize swarm cluster
become: yes
command: docker swarm init –advertise-addr={{ swarm_iface | default(‘eth0’) }}:2377
register: bootstrap_first_node
tags: [dockermanager]
mkdir -p ansibleSwarm/ansible/roles/dockermanager/tasks
—
– name: leaving older swarm
command: docker swarm leave -f
become: yes
ignore_errors: yes
tags: [swarm]
– name: join nodes to manager
command: docker swarm join –token={{ tokennode }} {{ hostmanager }}
become: yes
register: swarm_join_result
failed_when: not “‘This node is already part of a swarm’ in command_result.stderr”
tags: [swarm]
# mkdir -p ansibleSwarm/ansible/roles/dockerworker/vars
# vars file for dockermanager
tokennode: “{{ hostvars.manager.bootstrap_first_node.stdout.split(‘n’)[5].split(‘ ‘)[5] }}”
hostmanager: “{{ hostvars.manager.bootstrap_first_node.stdout.split(‘n’)[6] }}”
—
– name: Install common packages
hosts: swarm
roles:
– common
– docker
– name: Configure Manager
hosts: swarm-managers
roles:
– dockermanager
– name: Configure Workers
hosts: swarm-workers
roles:
– dockerworker
# ansible-playbook -i inventory/hosts swarm.yml
root@ip-172-31-52-216:/home/rodrigo/Development/innuy/AnsibleSwarm/ansible# ansible-playbook -i inventory/hosts swarm.yml
PLAY [Install common packages] *************************************************
TASK [setup] *******************************************************************
ok: [worker1]ok: [manager]
TASK [common : Clean yum] ******************************************************
changed: [worker1][WARNING]: Consider using yum module rather than running yum
changed: [manager]
TASK [common : upgrade all packages] *******************************************
ok: [worker1]ok: [manager]
TASK [common : Disable selinux] ************************************************
changed: [manager]changed: [worker1]
TASK [common : install unzip] **************************************************
ok: [worker1]ok: [manager]
TASK [common : Enable the EPEL repository definitions.] ************************
ok: [worker1]ok: [manager]
TASK [common : Install python setup tools] *************************************
ok: [worker1]ok: [manager]
TASK [common : Install Pypi] ***************************************************
ok: [worker1]ok: [manager]
TASK [common : install git] ****************************************************
ok: [worker1]ok: [manager]
TASK [docker : Install docker-py] **********************************************
changed: [worker1]changed: [manager]
TASK [docker : Update docker repo] *********************************************
ok: [worker1] => (item=[dockerrepo])
ok: [manager] => (item=[dockerrepo])
ok: [worker1] => (item=name=Docker Repository)
ok: [manager] => (item=name=Docker Repository)
ok: [worker1] => (item=baseurl=https://yum.dockerproject.org/repo/main/centos/7/)
ok: [manager] => (item=baseurl=https://yum.dockerproject.org/repo/main/centos/7/)
ok: [manager] => (item=enabled=1)
ok: [worker1] => (item=enabled=1)
ok: [manager] => (item=gpgcheck=1)
ok: [worker1] => (item=gpgcheck=1)
ok: [manager] => (item=gpgkey=https://yum.dockerproject.org/gpg)
ok: [worker1] => (item=gpgkey=https://yum.dockerproject.org/gpg)
TASK [docker : Install Docker] *************************************************
ok: [manager]ok: [worker1]
TASK [docker : enable sysv dockerd service] ************************************
ok: [worker1]ok: [manager]
TASK [docker : Start service] **************************************************
ok: [manager]ok: [worker1]
PLAY [Configure Manager] *******************************************************
TASK [setup] *******************************************************************
ok: [manager]
TASK [dockermanager : Disabling swarm] *****************************************
changed: [manager]
TASK [dockermanager : initialize swarm cluster] ********************************
changed: [manager]
PLAY [Configure Workers] *******************************************************
TASK [setup] *******************************************************************
ok: [worker1]
TASK [dockerworker : leaving older swarm] **************************************
changed: [worker1]
TASK [dockerworker : join nodes to manager] ************************************
changed: [worker1]
PLAY RECAP *********************************************************************
manager : ok=17 changed=5 unreachable=0 failed=0
worker1 : ok=17 changed=5 unreachable=0 failed=0
Hope this tutorial has helped you in combining Ansible and Docker Swarm for your needs!