APACHE WEBSERVER CONFIGURATION IN DOCKER USING ANSIBLE

Mangesh Ghorpade
6 min readDec 5, 2020

📌What is Ansible?

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intra service orchestration, and provisioning. Automation is crucial these days, with IT environments that are too complex and often need to scale too quickly for system administrators and developers to keep up if they had to do everything manually.

📌Why Ansible?

Ansible is an Agentless tool that means we don’t need to install it on the managed node just installed on the controller node and start working with ansible. Ansible is a very intelligent tool and its intelligence comes from modules. As an IT Admin, we don’t have to remember all OS commands just tell which package you wanna install and ansible will do that thing for you!

📌 Ansible Architecture :

◆What is the playbook file?

Playbooks are the files where the Ansible code is written. Playbooks are written in YAML format. YAML stands for Yet Another Markup Language. Playbooks are one of the core features of Ansible and tell Ansible what to execute. They are like a to-do list for Ansible that contains a list of tasks.

Playbooks contain the steps which the user wants to execute on a particular machine. Playbooks are run sequentially. Playbooks are the building blocks for all the use cases of Ansible.

◆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.

📌What is Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.

✨STEPS TO BE PERFORMED FOR THIS 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 httpd container and expose it to the public

- Copy the HTML code in /var/www/html directory and start the webserver

📌Now, let’s move on to the task

  • In Ansible Controller Node is also known as Manager Node and Managed Node is also known as Target Node .
  • First we have to create VM for Controller Node and Managed Node. As you can see below i am using one ControlNode and another ManageNode
  • In my case My Controller Node (Redhat8_GUI) has “192.168.0.100” IP and Target Node has “192.168.0.104” IP

*Prerequisites

First, we have to install Ansible in Manager Node

pip3 install ansible
ansible --version
mkdir etc/ansible
vim /etc/ansible/ansible.cfg
  • Create a directory ansible inside /etc/
  • Create a ansible configuration file.
vim MyHosts.txt
  • Create a host file anywhere i have created in root.
  • Transfer epel software using winscp
rpm -ivh epel-release-latest-8.noarch.rpm
  • Installing sshpass
yum install sshpass
  • Path updated automatically
ansible --version
/etc/ansible/ansible.cfg
  • Create inventory inside :
/etc/ansible/ansible.cfg
  • The Ansible inventory file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate. The file can be in one of many formats depending on your Ansible environment and plugins. The default location for the inventory file is /etc/ansible/hosts.
  • Host key checking must be false.
  • Host group
  • In this file, we add a system(Managenode) who want to configure by giving the IP, user name and password for configuring it.
vim MyHosts.txt
ansible all --list-hosts
  • To check with all Managed Nodes run this command “ansible all -m ping” . “ping” is module name in ansible. In my case, My ControlNode is connected with ManagedNode.
ansible all -m ping
  • Create a directory in my case i have named it as Workspace1 and write a playbook for apache webserver configuration in docker using ansible here it is “web.yml” and also write a html file here it is “index.html”.
mkdir Workspace1
gedit index.html
gedit web.yml
  • To run this file we have to run the below command
ansible-playbook web.yml
  • To check docker is configured and services are started in ManagedNode with “192.168.0.104” IP
ifconfig enp0s3
docker --version
  • To check container is launched
docker ps 
  • To check IP of ManagedNode we use
docker inspect httpd-web | grep IP

-Let’s check on browser

  • Finally, everything is working up and we got the result.

✨How to write a playbook for above task:-

In this way, Task is Completed Successfully !!

!! Thank You for Reading the Article !!

--

--