APACHE WEBSERVER CONFIGURATION IN DOCKER USING ANSIBLE PLAYBOOK

Siri Chandana
4 min readDec 20, 2020

Task description :

🔰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

Before going to above task we need to know some Prerequisites concepts:

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.

Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security allow you to run many containers simultaneously on a given host. It was first started in 2013 and is developed by Docker, Inc.

What is ansible ?

Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.

Designed for multi-tier deployments since day one, Ansible models your IT infrastructure by describing how all of your systems inter-relate, rather than just managing one system at a time.

🔰 Ansible Architecture :

♦️ Ansible Playbooks :

Ordered lists of tasks, saved so you can run those tasks in that order repeatedly. Playbooks can include variables as well as tasks. Playbooks are written in YAML and are easy to read, write, share and understand.

♦️ Inventory :

A list of managed nodes. An inventory file is also sometimes called a “hostfile”. Your inventory can specify information like IP address for each managed node. An inventory can also organize managed nodes.

♦️ Control Node:

Any machine with Ansible installed is known as controller node. You can run Ansible commands and playbooks by invoking the ansible or ansible-playbook command from any control node. You can use any computer that has a Python installation as a control node - laptops, shared desktops, and servers can all run Ansible. However, you cannot use a Windows machine as a control node. You can have multiple control nodes.

♦️ Managed Node:

The network devices (and/or servers) you manage with Ansible. Managed nodes are also sometimes called “hosts”. Ansible is not installed on managed nodes.

#To install the ansible 
pip3 install ansible
yum install sshpass
#To see version of ansible installed
ansible --version

To Check Connectivity with All managed nodes:

ansible all -m ping

👇 Lets Start Our Task

Create a file for playbook with extension .yml

And Create another file which will be copying inside the container.

👉 In Playbook

👉 now run the playbook

👉🏻 To check docker is configured and services are started in Managed Node :

docker --version
systemctl status docker

👉🏻 To check container is launched

docker ps

👉🏻 To check IP of Managed Node :

docker inspect httpd-web | grep IP

✨ Let’s check on Browser : ✨

So finally task completed !!!! 🤩🤩

Thanks You !!!

🔰 Keep Learning !! Keep Sharing !! 👍✌️

--

--