What is Ansible and How to configure apache webserver in Docker using Ansible?

Introduction to Ansible:
Ansible is an open source Automation platform. It is very, very simple to setup and yet powerful. Ansible can help you with configuration management, application deployment, task automation. It plays a vital role when you have the requirement to configure the same environment in multiple different systems either from the scratch or from the halfway since it uses Playbook (similar to script) and follows Idempotency !
Ansible Terminology and Architecture:

Control node — System where Ansible and Python installed. You can run Ansible commands and playbooks by invoking the ansible or ansible-playbook command from any control node.
Management nodes — The network devices or servers to manage with Ansible. Managed nodes or target nodes are the ones that are managed or controlled by the Control node.
Inventory — This is where the details of managed or target nodes are given.
Modules — Multiple small codes that Ansible executes (python files)
Tasks — specific actions to perform within a Playbook
Variables — something different about a specific host, This makes the playbook dynamic.
Playbooks — collection tasks written in YAML
Main Part: Configuring apache webserver in Docker using Ansible .
Requirements:
To 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
Setting up the Control Node for ansible
STEP 1: Download Ansible in your Control Node
pip3 install ansible
STEP 2: INVENTORY
→ Create a file with the details of the Managed nodes using the below format.
MN_IP ansible_user=<username> ansible_ssh_pass=<password> ansible_connection=ssh(connection name)
→ Include this file using the following code in the Ansible Configuration file

If you are setting up the control node for first time see that you have ssh pass installed, to do that you can use,
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
Then yum install sshpass
We can list all the hosts(managed nodes) by ansible all — list-hosts
Then check the connectivity by using ansible all -m ping

MAIN PART : (WRITING A PLAYBOOK TO CONFIGURE WEBSERVER IN DOCKER)
→Create a directory/workspace using mkdir
→Create a file with .yml extension inside this directory

Now let’s start writing in our Playbook




Play the Playbook
ansible-playbook <playbookname.yml>
WRITING A PLAYBOOK TO CONFIGURE WEBSERVER IN DOCKER IS SUCCESSFULL !
Now let’s check whether all our requirements are satisfied….
Go to your Managed node
1) To check whether docker configured, started and enabled….
systemctl status docker

2)to check whether httpd server image launched and running….

3) To check whether html code copied and exposed to public and webserver started… (MN IP:Port no/filename.html)

