Introduction
Hi everyone! Today in this blog, we will be installing Docker by creating an Ansible role.
Roles are a feature in Ansible that simplifies the playbook. We can break a complex Ansible playbook into fully independent or interdependent collections of files, tasks, templates, variables, and modules. This enhances reuse-ability and promotes modularisation of configuration. We can create an Ansible role using the following command –
$ ansible-galaxy init <role_name>
Now, let’s follow a step by step approach to create our role to install Docker.
1. Create a directory
$ mkdir ansible && cd ansible
2. Create a role
$ ansible-galaxy init install_docker

The role structure would look like this –



3. Write yaml files
Now, let’s start writing our yaml files one by one. First, change directory to tasks.
$ cd tasks/
Then create a file installPackages.yml
which will install all the required packages. Write the following code in it –
- name: Install required packages
yum:
name: "{{ item }}"
state: latest
with_items:
- yum-utils
- device-mapper-persistent-data
- lvm2
Next, create a file addRepo.yml
which will add and enable the official docker repository. Write the following code in this file –
- name: Add Docker repo
get_url:
url: "{{ docker_repo }}"
dest: /etc/yum.repos.d/docer-ce.repo
- name: Enable Docker Edge & Test repo
ini_file:
dest: /etc/yum.repos.d/docer-ce.repo
section: "{{ item }}"
option: enabled
value: 0
with_items: ['docker-ce-test', 'docker-ce-edge']
Next file into the line is installDocker.yml
which will install and start the docker service. It contains the following code –
- name: Install Docker
package:
name: docker-ce
state: latest
- name: Start Docker service
service:
name: docker
state: started
enabled: yes
Lastly, let’s write a file named addUser.yml
to add the user. The code is –
- name: Add user vagrant to docker group
user:
name: vagrant
groups: docker
append: yes
So, now we have 4 yaml files to get our task done. Now, let’s add the following code in the main.yml
inside the same directory –
---
# tasks file for install_docker
- import_tasks: installPackages.yml
- import_tasks: addRepo.yml
- import_tasks: installDocker.yml
- import_tasks: addUser.yml
Now, let’s change our directory to vars
and put the following code into its main.yml
–
---
# vars file for install_docker
docker_repo: "https://download.docker.com/linux/centos/docker-ce.repo"
4. Write the main playbook
We now just have to write a main playbook to use this ansible role. It should be created outside the install_docker
role’s directory. Let’s name this docker.yml
and write the following code in it –
---
- name: Install docker on RHEL
hosts: localhost
become: yes
roles:
- install_docker
We’re done writing the code. Now, the code structure should look like this inside the ansible
directory –



Finally, let’s run this playbook with the following command –
ansible-playbook docker.yml
If everything has been done correctly, we should see the following output –



I hope this article was informative and useful to you. Please feel free to drop any comment, question, suggestion or improvement.