Installing Docker using Ansible playbook in CentOS7

In this article, we are going to install Docker Community edition in CentOS7 using Ansible playbook.

Here I have used 2 VMs as below,

1) VM (ansible-controller) as Ansible controller machine
2) VM (cent7-node-1) as managed node where we have to install Docker

Ansible controller machine has Ansible with 2.5.5 version as below,

[ansible@ansible-controller ~]$ ansible --version
ansible 2.5.5
config file = /home/ansible/ansible.cfg
configured module search path = [u'/home/ansible/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /bin/ansible
python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
[ansible@ansible-controller ~]$

[ansible@ansible-controller ~]$ pwd
/home/ansible
[ansible@ansible-controller ~]$ ls
ansible.cfg hosts
[ansible@ansible-controller ~]$

Anisble controller node has been configured with inventory of ‘hosts‘ file with node of ‘cent7-node-1’

[ansible@ansible-controller ~]$ cat hosts
cent7-node-1
[ansible@ansible-controller ~]$

We can test the connectivity between ‘ansible-controller’ and managed node ‘cent7-node-1’ via PING module as below,

[ansible@ansible-controller ~]$ ansible -m ping all
cent7-node-1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[ansible@ansible-controller ~]$

Now we can write a Playbook ‘install_docker.yml‘ to install the Docker as below,

[ansible@ansible-controller ~]$ cat install_docker.yml
---
- hosts: cent7-node-1
user: ansible
become: yes
tasks:
- name: "Installing Docker Prerequisite packages"
yum:
name: "{{ item }}"
state: latest
with_items:
- yum-utils
- device-mapper-persistent-data
- lvm2
- name: "Configuring docker-ce repo"
get_url:
url: https://download.docker.com/linux/centos/docker-ce.repo
dest: /etc/yum.repos.d/docker-ce.repo
mode: 0644
- name: " Installing Docker latest version"
yum:
name: docker-ce
state: present
- name: " Starting and Enabling Docker service"
service:
name: docker
state: started
enabled: yes
[ansible@ansible-controller ~]$

I have used four tasks,
1)- name: “Installing Docker Prerequisite packages”
To install the pre-requisite packages using ‘YUM’ module
2)- name: “Configuring docker-ce repo”
To download ‘docker-ce.repo’ file to ‘/etc/yum.repos.d’ directory using ‘get_url’ module
3)- name: ” Installing Docker latest version”
To install docker using ‘YUM’ module
4)- name: ” Starting and Enabling Docker service”
To start and enable the docker service using ‘service’ module

After writing playbook, we can verify the syntax of the playbook using below command,

[ansible@ansible-controller ~]$ ansible-playbook install_docker.yml --syntax-check

playbook: install_docker.yml
[ansible@ansible-controller ~]$

Before running playbook in managed node(cent7-node-1) status:

There is no Docker repository file under ‘/etc/yum.repos.d/’ and no docker package is available as below,

[root@cent7-node-1 ~]# cd /etc/yum.repos.d/
[root@cent7-node-1 yum.repos.d]# ll
total 32
-rw-r--r--. 1 root root 1664 May 17 09:53 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 May 17 09:53 CentOS-CR.repo
-rw-r--r--. 1 root root 649 May 17 09:53 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 May 17 09:53 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 May 17 09:53 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 May 17 09:53 CentOS-Sources.repo
-rw-r--r--. 1 root root 4768 May 17 09:53 CentOS-Vault.repo
[root@cent7-node-1 yum.repos.d]#
[root@cent7-node-1 yum.repos.d]# rpm -qa | grep -i docker
[root@cent7-node-1 yum.repos.d]#

Running Playbook:

We can execute our playbook ‘install_docker.yml’ as below,

[ansible@ansible-controller ~]$ ansible-playbook install_docker.yml

PLAY [cent7-node-1] ***********************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [cent7-node-1]

TASK [Installing Docker Prerequisite packages] *************************************************************************************************************************
changed: [cent7-node-1] => (item=[u’yum-utils’, u’device-mapper-persistent-data’, u’lvm2′])

TASK [Configuring docker-ce repo] **************************************************************************************************************************************
changed: [cent7-node-1]

TASK [Installing Docker latest version] ********************************************************************************************************************************
changed: [cent7-node-1]

TASK [Starting and Enabling Docker service] ****************************************************************************************************************************
changed: [cent7-node-1]

PLAY RECAP *************************************************************************************************************************************************************
cent7-node-1 : ok=5 changed=4 unreachable=0 failed=0

[ansible@ansible-controller ~]$

After executing playbook– managed node status:

[root@cent7-node-1 yum.repos.d]# ll
total 36
-rw-r--r--. 1 root root 1664 May 17 09:53 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 May 17 09:53 CentOS-CR.repo
-rw-r--r--. 1 root root 649 May 17 09:53 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 May 17 09:53 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 May 17 09:53 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 May 17 09:53 CentOS-Sources.repo
-rw-r--r--. 1 root root 4768 May 17 09:53 CentOS-Vault.repo
-rw-r--r--. 1 root root 2424 Jun 25 23:47 docker-ce.repo
[root@cent7-node-1 yum.repos.d]#

Here ‘docker-ce.repo’ file has been downloaded and ‘docker-ce’ package has been installed

[root@cent7-node-1 yum.repos.d]# rpm -qa | grep -i docker
docker-ce-18.03.1.ce-1.el7.centos.x86_64
[root@cent7-node-1 yum.repos.d]#

Docker service is active and in running state as below,

[root@cent7-node-1 yum.repos.d]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2018-06-25 23:48:14 EDT; 4min 43s ago
Docs: https://docs.docker.com
Main PID: 2204 (dockerd)
Tasks: 16
Memory: 39.5M
CGroup: /system.slice/docker.service
├─2204 /usr/bin/dockerd
└─2208 docker-containerd --config /var/run/docker/containerd/containerd.toml

Jun 25 23:48:13 cent7-node-1 dockerd[2204]: time=”2018-06-25T23:48:13-04:00″ level=info msg=serving… address=”/var/run/docker/containerd/docker-container…nerd/grpc”

Jun 25 23:48:14 cent7-node-1 systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
[root@cent7-node-1 yum.repos.d]#

Hence, we have installed and started the docker in managed node ‘cent7-node-1’ via our Ansible playbook.

 

Ref.source: https://docs.docker.com/v17.09/engine/installation/linux/docker-ce/centos/#install-docker-ce-1

 

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: