Ansible, Docker configuration, and Httpd container
This blog will help you in understanding Ansible’s configuration management power to solve the use case of running a web server on top of a Docker container.
In this article, I’ll show how to do the following tasks:
Writing 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
Before starting with the actual task, we need to meet the pre-requisite for the task, which includes Installation of Ansible, Setting up the config file for Ansible, and setting up Ansible Inventory. Also, I’m assuming that Managed node has yum already configured. Although that would not be required for installing docker.
Let’s start with installing ansible. It is written in python language as a package, So, can be installed using Pip.
Check ansible version
Creating Ansible Inventory
IP of Managed Node
We can add more Managed Node IP addresses in the inventory according to our needs.
Now we’ll start with the Tasks.
Create a yaml file (playbook) with name configure_docker.yml
Write the code (play) to create a yum repository for docker and to install the docker-ce package.
Run the file with the command:
The second task of installing package gives error as docker-ce require an option of “ — nobest” in the command.
We don’t have this attribute available in the module, so we will use the command module to install docker-ce.
Run the playbook
So, our Docker-ce (Community Edition) is installed successfully now.
Start and enable Docker services
Write task in playbook to start and enable service.
Run the playbook, you’ll see the task being executed.
Cross-check on the Managed Node (RHEL8 OS 2)
Pull the httpd server image from the Docker Hub
Write task in playbook to pull httpd server image. I already have one image on CENTOS+HTTPD, so I’ll use that.
We also need to have Docker SDK for Python, which we will be installing using pip module of ansible.
Both tasks run successfully now.
We can cross-check from Managed Node.
Run the docker container and expose it to the public
For this, we will use docker_container module.
Now run the playbook.
Cross-check on Managed Node
This server can be accessed from Managed Node’s IP and PORT Number 1234, on which PATing (Port Address Translation) has been done to PORT 80 of Docker container.
Copy the Html code in /var/www/html directory
and start the webserver
Now, we’ll copy the html code to be hosted using copy module of ansible to copy onto the Managed Node and then we’ll use command module to copy into the destination folder of /var/www/html which is the Document Root for Apache Web Server.
Let’s finally run the ansible-playbook one last time and our Task will be finished.
Now, as the port is exposed to the Public, we can access the web page from the windows OS.
This was all about this task.
Thank you… :)