|| Automatize the Docker with Ansible Playbook || Ansible + Docker => Make Docker configuration smooth.. ||
By: Radhika Sharma
Tags: Ansible, Ansible Playbook, Docker, Automation

Achieve Intelligence in Configuration using Ansible..

In this blog I will tell you how we can use Ansible to launch the Docker Container and can configure apache web server inside it.

Task:-

🔹 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.

Step1 : Install Ansible

As we know, Ansible is a tool for configuration Management that is built on Python, can consider as a python library so we can install it via pip command, condition is that in your system python should be installed , in my case it is already installed so I am using pip3 command directly to install it.

pip3 install ansible

We also need to install sshpass program in our local system or in controller node , because ansible will use SSHprotocol to go in target node.

yum install sshpass -y

Step2 : Create Inventory file

Ansible works against multiple managed nodes or “hosts” in your infrastructure at the same time, using a list or group of lists known as inventory. Once your inventory is defined, you use patterns to select the hosts or groups you want Ansible to run against.

in your infrastructure at the same time, using a Once your inventory is defined, you use patterns to select the hosts or groups you want Ansible to run against. Inventory file we can create anywhere (Default Locaion is /etc/ansible/hosts )in the Controller Node (The system in which ansible is installed ), and it will mainly consist the few things about Target Node or Managed Node or host system :

)in the Controller Node (The system in which ansible is installed ), and it will mainly consist the few things about : Ip address of host system, via keyword ansible_user -> Username give the user name , via keyword ansible_ssh_pass give Password , ansible _connect ->Protocol to be used to enter in Target node.

In my case inventory file I created in /ansible folder, you can create anywhere & can give any name to your file

Step 3: Create Configuration File of Ansible

We have to create the configuration file for Ansible in /etc/ansible folder with name ansible.cgf , now give here location of inventory file. In my case it was /ansible/inventory.txt

Step 4 : Just Check .. Is everything working fine?

Check the list of hosts present in inventory file.

ansible all --list-hosts

I have only one host in my inventory file , so it is showing me the Ip of that host(or Target node)

Check if there is network connectivity between target node and controller node or not.

ansible all -m ping

output shows that we have network connectivity because it is pingable..

Step 5 : Write Ansible Playbook

Ansible use YAML language to create its playbook . When we run our playbook it will perform all the tasks on managed node according to provide information written in playbook

we are assuming that in host system , yum is also not configured and dvd is also not mounting to any folder, so we have to do a lots of operation there in that case in host system.

Ansible use modules to perform tasks on managed node. These are the one who is able to perform various operation via running multiple commands respective to the OS in target node . For more see here.

These are the one who is able to . So I used a lots of modules according to the task we have to do.

First create directory /root/dvd Then , mount the cdrom or dvd(/dev/cdrom )in this folder . Now create yum repository for AppStream and BaseOs folders. (Note : I am using RHEL-8 here as a target node and managed node..so I am configuring yum and performing all the task accordingly .) Create yum repository to install docker-ce because we have not proper support of docker-ce in RHEL-8, so we are using CentOS online repository to download the software.

5. Now install docker.

6. start docker service and enable it.

7. Install python3 package because we need to install docker sdk via pip in host system to launch docker container.

8. Now copy all the web pages, which we want to configure in web server inside the docker container from controller node to target node. Here target node will be Docker host on which container is running.

9. Now launch docker container with having httpd server configured so for that : pull httpd image from docker hub and from it launch docker container , expose the port 80 because on this port in container web server is running , and use volume module to link folder in which we earlier copied the web pages (In my case it was /htmlfiles)in target node to document root of web server (i.e. /usr/local/apache2/htdocs/ in httpd image.).

Next, we want to create a web server inside the docker container that should be accessed from outside world but docker container is isolated from outside environment , The network connectivity is can be possible between docker container and docker host , so we have to use a concept port forwarding , so that if we access any port on docker host , it will be redirected to the port 80 of docker container on which web server is running. This creates a firewall rule which maps the container port (i.e. 80)to a port on the Docker host (i.e. 7000 in my case, you can use any in the port range)to the outside world.

10. we expose the container port 80 to the 7000 port of docker host (on which docker container is running , that is also our target node) so we need to set the firewall rules for 7000 port of target node.

Note : we also have to set some masquerading rules for port forwarding in linux (In my case it is RHEL-8 ) i.e our target node, so I used this command manually in my host system to make it simple, we can also write code in ansible playbook for it.

# Masquerading allows for docker ingress and egress

▪️firewall-cmd --zone=public --add-masquerade --permanent # Specifically allow incoming traffic on port 80/443

▪️firewall-cmd --zone=public --add-port=80/tcp

▪️firewall-cmd --zone=public --add-port=443/tcp # Reload firewall to apply permanent rules

▪️firewall-cmd --reload #Restart docker

▪️systemctl restart docker

Here is the complet code for ansible playbook.

Step 6 : Run the Playbook.

First Check syntax..