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
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.
Ansible is an Agent less 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!
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.
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.
In Ansible Controller Node is also known as Manager Node and Managed Node is also known as Target Node.
In my case My Controller Node (Redhat8_GUI) has “192.168.43.253” IP and Target Node has “192.168.43.154” IP
Now, let's move on to the task
First, we have to install Ansible in Manager Node
pip3 install ansible
Ansible hosts.txt file
In this file, we add a system(manage node) who want to configure by giving the IP, user name and password for configuring it. Basically its path is /etc/hosts.txt. In this task I’ll be using test node
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.
To check with all Managed Nodes run this command “ansible all -m ping” . “ping” is module name in ansible. In my case, My Controller Node is connected with Managed Node.
Now the setup of the Ansible is done here. So, we can go to the next step that is creating our own ansible-playbook. Ansible Support YAML language in the playbook.
1.Creating a Yum repository
For installation of docker, first, we have to configure yum which allows us to automatically install or update packages.
To run this file we have to run the below command
2.Creating a Yum repository for Docker and to enable docker services
Now we need to download docker from the provided URL and to tell yum to install docker. After installing docker, we have to start our docker services which we perform with the help of a service module
3. Build DockerImage and to copy HTML, DockerFile to Manage Node
Now we have to build a docker image and launch a container using that image which has web-server configured. It uses centos as a base OS Also, we’ve installed httpd server and started its services. And I’ve created an html webpage to see if everything works properly.
Copy module copies the Dockerfile and html file which I’ve created to the path mentioned and modules docker_image pulls the centos image that we mention in our docker file and perform commands which we specified. src is the place where the file is present and dest is the one where copy module copies the file.
There is a requirement of the docker-py package so we have to install it using pip.
Now the last one is to launch the container using the image where we configured httpd image. docker_container module launches the container from the image webpage which we built using docker file. And we exposed it to port number 1234 for testing.
Finally, everything is working up and we got the result.
You can find the entire code in my GitHub repo