Configured of “httpd” Apache web server in Docker Container Using Ansible Playbook.

Configured of “httpd” Apache web server in Docker Container Using Ansible Playbook.

Hello Everyone,

In this article, we have successfully configured HTTPd apache web server with the combination of two trending technologies namely Ansible, Docker

Before start implementation, a good thing is to introduce the technologies which we have used to complete this article.

What is Ansible?

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. Know more

Let’s have a look at some of the terminology used in Ansible:

  • Controller Node: Machine where Ansible is installed.
  • Managed Node: Managed nodes are also sometimes called hosts.
  • Inventory: Information regarding servers to be managed
  • Playbook: Automation is defined using tasks defined in YAML format
  • Task: Procedure to be executed
  • Module: Predefined commands executed directly on remote hosts.

What is Docker?

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. Know more

This article contains the following tasks:

🎇Configure Docker

🎇 Start and enable Docker services

🎇 Pull the httpd server image from the Docker Hub

🎇 Run the httpd container and expose it to the public

🎇 Copy the html code in /var/www/html directory and start the Web-Server.

Pre-Requisite to configuring httpd in the docker container.

  1. Pre-installed Ansible in Controller Node

So first we complete the requisite that “How to Install Ansible”???

Before install ansible makes sure your system has python because ansible built on top of python to must have python in your system. So let’s check the python is there or not with the command —

python3 — version

If you see on CMD like this or any version of python then you have python-

Python 3.6.8

If any case you don’t have python then follows the given command to install python in RHEL8

yum install python3

The output will be —

No alt text provided for this image

When Python installed successfully then we will install ansible using the following command-

pip3 install ansible

The output will be-

No alt text provided for this image

In my case, I’ve already so I got requirement satisfied in the output your output will be different if you install the first time.

Now we’ll check ansible installed or not or maybe the system does kidding with us🤪 it may not install python after we’ve run successfully command.

No alt text provided for this image

Yeah,😀 We’ve successfully install ansible with version 2.10.3. Finally, the system working fine😜

Creating Inventory file “ip.txt” and “ansible.cfg” file

  • After installing ansible create an Inventory anywhere in the system, here I have created an inventory named “ip.txt”
  • Now we have to provide details of the managed node in this file created name is “ip.txt” in my case. Here I am Using RHEL8 Os as my Managed Node.
No alt text provided for this image

Let’s understand vocabs that are used ion inventory file name is “ip.txt”

  • Here, “192.168.43.44” is the IP of Managed Node
  • ansible_ssh_user: User you want to login in managed node
  • ansible_ssh_pass: provide the password of the above user
  • ansible_connection: The medium of connection using this both are connecting to each other.

After that, create a directory where we’ll need to create ansible.cfg file using the following command —

mkdir /etc/ansible
cd /etc/ansible

Now create file name is ansible.cfg in the directory “/etc/ansible” using the following command —

vim ansible.cfg

Writing the path of the inventory file in ansible.cfg like this —

[defaults] 
inventory=/root/ip.txt
depracation_warnings=False
host_key_checking=False
command
No alt text provided for this image

Now check the connectivity with Managed Node using the following command —

ansible all -m ping
No alt text provided for this image

If it shows a message in green color that means there is connectivity and you will see the message ping pong.

Now create yml file, here i have created file name docker.yml. This yml file is a playbook where we write code in YAML.

The code in the playbook is as below:

No alt text provided for this image

Before running the ansible-playbook we can check the yum repository of docker and docker version to confirm we have not both the configuration before running the ansible-playbook.

To check docker repo we go to the path — where we check docker. repo present or not?

cd /etc/yum.repos.d/

No alt text provided for this image

Till now we don’t have any docker repo.

As well as, we check the docker available or not for the check we use the following command —

rpm -q docker-ce
No alt text provided for this image

Till now package docker-ce is not installed

Now run the playbook using the following command —

ansible-playbook docker.yml
No alt text provided for this image

Here, If we see green color then it means that much code is skipped, if we orange then some changes are made and if we don’t see any red color in output then it means The playbook is executed successfully.

Now we can verify that docker is installed in the Managed Node.

No alt text provided for this image

Also, Docker repo created in /etc/yum.repos.d

Now we’ll use our client that is any browser in the search bar of any browser we type our URL(IP:port/htmlpage).

No alt text provided for this image

Now finally So the webpage is working well and showing the content properly whatever I write in the index.html file.

I hope you got an idea about how to configure the web server on the top of the docker using Ansible. If you have any problem with the playbook then you check in the Github repository.

Thank You So Much For Reading😊






To view or add a comment, sign in

More articles by Umesh Tyagi

Insights from the community

Others also viewed

Explore topics