Integration of Ansible with docker

Priyanka Gavali
5 min readJan 14, 2022

Task Description📄

🔰Write an Ansible PlayBook that does the following operations in the managed nodes:

🔹 Configure Docker

🔹 Start and enable Docker services

🔹 Pull the httpd server image from the Docker Hub

🔹 Run the docker container and expose it to the public

🔹 Copy the html code in /var/www/html directory and start the webserver.

This is an important article on the creation of an ansible-playbook. In this article, I am going to explain the Creation of an ansible-playbook to do all the 5 operations on other node called managed node by creating a playbook on the controller node.

Before moving to the task let us see some important things:

What is ansible???

In simple words, Ansible is an open-source IT automation engine, which can remove drudgery from your work life, and will also dramatically improve the scalability, consistency, and reliability of your IT environment. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy.

What is Docker???

In simple words, Docker is an open-source platform for building, deploying, and managing containerized applications. Docker is used as an alternative because they are more lightweight in terms of resources than virtual machines. Containers share operating systems whereas virtual machines are designed to emulate virtual hardware.

So, here we start with our task:-

Prerequisites:

🌀Controller Node

🌀Managed Node

In my case, I am using RHEL8 for both controller as well as a managed node.

I am using the controller node having IP address 192.168.43.152 and managed node having IP address 192.168.43.126.

Step 1: Python3 should be installed on your controller node in order to install ansible.

In my case, python3 is already installed. we can check its version by using the command:

python3 --version

Step 2: Now, let’s install ansible using the command:

pip3 install ansible

Step 3: Now let’s Check Ansible Version using the command:

ansible --version

You can see there is no config file.

Step 4: Now, Let’s Create inventory file. For this, we need the IP of Target Node and then also put USER NAME & PASSWORD in the Ansible inventory file

vim nodeips.txt

then put the credentials of managed node(slave node)

Step 5: Now let’s update the configuration file of the ansible.

Now create the directory /etc/ansible then create ansible configuration file i.e ansible.cfg inside folder /etc/ansible edit the configuration file of ansible i.e ansible.cfg

vim /etc/ansible/ansible.cfg

then update the file with the following:

Now let’s check the list of the Hosts and the Connectivity with your Target Node. Command:-

ansible all -m ping

OOPS!!! it’s giving an error. Let’s install sshpass program.

Step 6: for this, we need to install epel-release

Note: Epel repo Provides lots of open source packages to install via Yum and DNF. Epel repo is 100% open source and free to use. It does not provide any core duplicate packages and no compatibility issues. Instead of doing a manual process of configuring yum, EPEL tool available online will configure yum automatically.

Now let’s Install EPEL:

sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

Step 6: Now Let’s Install sshpass using epel

Step 7: Now check the list of the Hosts and the Connectivity with your Target Node. Command:

ansible all -m ping

Now all setup has done and our Controller node also has connectivity with our Target Node. Let’s create a Playbook now. Command:-

vim docker.yml

Step 7: Now let’s try the task of configuring docker step by step.

Here I have used some module of Ansible which are as follows:

yum_repository ( For creating Docker Repo in Yum Configuration file in our Slave Node

Package: For installing the docker software),

service ( For starting the Service of Docker),

pip (To install the Docker Python),

docker_image ( For downloading the docker image from the Docker Hub),

copy (To copy my web page from Master (controller) to Slave(Target),

docker_container (For starting the docker webserver container).

Step 8: Now we have to make our Web page in the same directory.Command:-

vim webpage.html

Step 9:Now let’s check our task by using the command :

ansible-playbook docker.yml

You will get the following output if your code has no errors

Step 10: Verification at managed node- after running the ansible-playbook we can see that here docker has installed.

Now let’s check our webpage on the browser fetch the IP of the Docker Container on firefox we have to use IP of docker container (my_os1) i.e 172.17.0.2 and port no 81.

Now fetch the IP of the Docker Container and use the URL to see your web page:-

http://172.17.0.2:81/webpage.html

Successfully completed the task!!!

Thanks for reading the article.

--

--