🌀Configuration of docker 🐳using ansible 💻

Priyanka Hajare
6 min readMar 14, 2021

🎇 Task description -

We have to 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 web server

🔰 What is ansible ?

Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. 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. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy

In ansible the node which we do configuration from is Controller Node and the node where we do configuration are Managed nodes.

For this task

I am using controller node having ip address 192.168.43.54

and managed node having ip address 192.168.43.72

🔰 Prerequisites —

♦ For our task we require two systems i.e one is controller node and one managed node for this you can install RHEL8 or any other Linux distribution .(In my case I am using RHEL8 for both controller as well as managed node.)

♦ -Ansible should be installed in Controller node.

📍 Steps to install ansible and configure ansible -

  1. Install python3 (In my controller node python has installed already)and check it’s version

2. Now install ansible. (ansible is written in python hence use pip3 to install ansible.)

3. To Check Ansible Version

4. Let’s Create inventory file

creating inventory file
  • Write the inventories or the managed nodes you have (👇inside inventory file)

⦁ Inside file we gave IP, username, password and protocol.

here,

IP address of managed node = 192.168.43.72

ansible_ssh_pass: provide the password of the above user

ansible_connection = ssh (controller node connect to managed node using ssh plugins)

5.Creating Configuration File Of Controller Node

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

we have to mention following things in ansible.cfg file

  • path of inventory file

6. Now let’s Install EPEL:

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

by using above command we can install EPEL

  • Why we need EPEL ?

👉 Instead of doing manual there EPEL tool available that will configure yum automatically

7. now Let’s Install sshpass using epel

⦁ In my case I already installed sshpass.

8. now check connectivity of controller node to target node

yes it is connecting…🤩

Now , We have configured ansible on controller node succesfully.

📌 Now let’s create playbook for task

I have created separate files i.e yum.yml , docker.yml , import.yml and index.html

  1. yum.yml is for configuration of yum on managed node
  2. docker.yml is for configuration of docker and to configure webserver(httpd) on container launched using docker .
  3. index.html — this is webpage
  4. import.yml — in this file I have imported all files mentioned above

In this case we only have to run import.yml (as show below)which will do all desrved task for us

✅ Inside yum.yml

✅ Inside docker.yml file

  • To configure yum for docker repo using yum_repository module
  • To install docker and to start the services of docker using package and service module respectively.
  • To copy file i.e index.html from controller node to root document of httpd(/var/www/html) which is present on base o.s

content of Index.html file

  • Now to install docker sdk for docker engine API using docker-py module and then I pulled httpd image from docker hub using docker_image module
  • To stop firewall and filter iptable.
  • To restart docker services if sevices are inactive
  • To launch docker container using httpd image and expose it for public access
  • By using volumes module we have attached /var/www/html folder of base OS to the folder (/usr/local/apache2/htdocs/) which present on launched container to copy html file into document root of apache present on docker container (i.e. webtask2)

Before running playbook docker was not installed on managed node

✅ After this run playbook i.e import.yml (in my case it is import.yml)

by using command :- ansible-playbook import.yml

Playbook run successfully 🤩 it means our task has done whatever we required on the top of managed node 🤩

✅ Verification at managed node- after running plabook we can see that here docker has installed and we already have seen that before running playbook there was not docker at managed node

📌Now let’s check our webpage (index.html)from browser (firefox)….

for opening webpage on firefox I used IP of docker container (webtask2) i.e172.17.0.2 and port no 80

(used url - http://172.17.0.2:80/index.html)

Now verification done Finally task completed successfully..! 😊

--

--