Configuration of Apache Webserver in Docker Using Ansible PlayBook


Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

Ansible playbook

An Ansible playbook is a blueprint of automation tasks — which are complex IT actions executed with limited or no human involvement. Ansible playbooks are executed on a set, group, or classification of hosts, which together make up an Ansible inventory. Ansible playbooks are essentially frameworks, which are prewritten code developers can use ad-hoc or as starting template. Ansible playbooks are regularly used to automate IT infrastructure (such as operating systems and Kubernetes platforms), networks, security systems, and developer personas.


Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

Apache Web Server

Apache HTTP Server is a free and open-source web server that delivers web content through the internet. It is commonly referred to as Apache and after development, it quickly became the most popular HTTP client on the web. It’s widely thought that Apache gets its name from its development history and process of improvement through applied patches and modules but that was corrected back in 2000. It was revealed that the name originated from the respect of the Native American tribe for its resiliency and durability.


🔰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

Installing and configuring Ansible.

We are using RHEL8 OS for this task. We use pip3 version of pip installer for python3, which downloads and configures new python modules. The command to install Ansible is:

pip3 install ansible

After installation, we need to configure the ansible. We need to list all the managed nodes using their IP, user-name and password. We create a file like ip.txt and write the required info.

vim /root/ip.txt

We then configure the ansible configuration files to use ansible software. We create a directory /etc/ansible and create a configuration file ansible.cfg

# To create the ansible directory:
mkdir /etc/ansible
# To create the ansible configuration:
vim /etc/ansible/ansible.cfg

We need to download sshpass o run SSH using the keyboard-interactive password authentication mode, but in a non-interactive way.

yum install sshpass

Ansible Playbook of the task

To the run the playbook

# ansible-playbook <name-of-the-file>.yml
ansible-playbook docker-webserver.yml

As we can see in the last line, the playbook was successful. We have successfully done our task. Here is the final output:

Thank you for reading.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store