Configuring httpd server on docker: Ansible Playbook

Prithviraj Singh
4 min readJan 9, 2021

--

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.

The Apache HTTP Server Project is an effort to develop and maintain an open-source HTTP server for modern operating systems including UNIX and Windows. The goal of this project is to provide a secure, efficient and extensible server that provides HTTP services in sync with the current HTTP standards.

Ansible is a revolutionary yet simple IT automation engine that automates cloud provisioning, configuration management, application deployment, etc. It is designed for multi-tier deployments, Ansible models your IT infrastructure by describing how all of your systems inter-relate, rather than just managing one system at a time.

Now wouldn’t it be fun to integrate all three of these technologies to get webpage that can be configured automatically over docker…

Task:

🔰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

Now to integrate docker with httpd we can simply use the docker container image i.e. official httpd image by apache.

Onwards to using Ansible to configure docker…

To do this we’ll need to use Ansible PlayBook. If Ansible modules are the tools in your workshop, playbooks are your instruction manuals, and your inventory of hosts are your raw material. By the help of a PlayBook we can write plays, which can be that be played out by ansible in a step by step manner. Ansible PlayBook reduces the work of using Adhoc commands, which though easy to use but aren’t very handy when we want to perform a task repetitively.

So let’s first look what are the requirements of using Ansible.

To use Ansible we require to:

  1. Install Ansible… To do simply use the command: pip3 install ansible (to use this command you’ll first need to have python3 in your OS)
  2. Write a 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. To do this write a file in your workstation folder like:
Inventory which I used

3. Write a configuration file for Ansible… Certain settings in Ansible are adjustable via a configuration file (ansible.cfg). To do this create a file named ansible.cfg which looks something like…

Configuration file that I used

As we can see here we can give the path of our inventory which will be used by Ansible.

So now our setup is done, we can even check Ansible by Ansible’s ping

Here green color represents successful ping(task). We can also see the reply from the Target Node, “pong” — Target Node, 192.168.29.19

Hence our setup has been done successfully, and Ansible is able to send requests to our Target. So now we can proceed to write our PlayBook which will be used by Ansible to configure Docker and launch HTTPD server container.

To write a playbook we need to create a YAML type file, which can be made in the same workstation. Use the command: vim <file name>.yml . This file can now be used to write our Adhoc commands in form of play using YAML syntax.

The PlayBook which we are going to use looks like:

Part1
Part2
Part3

Now we can give this PlayBook to Ansible and ask it to run it for us. To do this use the command: ansible-playbook <file name>.yml And it will give us a result which looks something like:

Please ignore the warnings

Once we are able to see the prompt again we can be sure that or playbook has been ran completely. At this point any red output suggest error which needs to be fixed.

Now, since we have configured httpd web-server over a container we should be able to see it’s output over the browser so let’s check that out…

The index page
The test page

So that’s it…

Task completed Successfully…🥳🥳\( ̄︶ ̄*\))

Thanks for reading( ゚д゚)つ Bye

--

--

No responses yet