Integration of Ansible with Docker
!! Hola Connections !!
Welcome you all to my First article which is based on TASK 1 of RH-294 Linux Automation using Ansible Training.
As we know when we work in heterogeneous environment we face many difficulties.We don't know which command that server supports. There are many different types of Operating Systems which has different commands to do operations.
So here is solution for all of this kind of questions and difficulties that we face.
TASK DESCRIPTION:
Write an Ansible Playbook that do following operations in managed node.
- 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
What is Ansible??🤔🤔
Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment,etc.Ansible is agentless, temporarily connecting remotely via SSH to do its tasks.
What is Docker??🤔🤔
Docker is the most popular file format for Linux-based container development and deployments. If you’re using containers, you’re most likely familiar with the container-specific toolset of Docker tools that enable you to create and deploy container images to a cloud-based container hosting environment.
Automate Ansible with Docker🤩🤩
In this task we automate ansible with docker using ansible playbook.We install docker and start services using automation.
TASK COMPLETION:
Pre-requisite for this task :
1. Two RHEL OS on your system with yum configuration.
- Controller Node
- Managed Node
So let's start for this task.
1.First we launch one OS on which we install ansible and worked as a Controller Node .
Then we check that python is installed or not.If python is installed then we install ansible.
#python3 -V -Command for checking whether python is installed or not.
#pip3 install ansible -Command for installing ansible.
#ansible --version -Command for checking ansible version.
2. For the target node we can launch one managed node.
Ansible knows about its target node from inventory file that we have to create inventory and give the path of that inventory file to ansible configuration file -- > /etc/ansible/ansible.cfg.
To see all target nodes run command -
#ansible all --list-hosts
To see all nodes are connected or ping run command -
#ansible all -m ping
3.Now our system is configured.So let's start to write code in one ansible playbook.
We use YAML language to code in ansible playbooks .As all the target work that are going to be done by ansible are divided into different tasks which form a play .
First we add one docker repository to yum repository in managed node.
Let's see our repository add or not in managed node.It's added.
4.Then we have to install docker package having stable version using command and to start and enable the docker services using service module.
5. To use ansible docker module we need to install docker SDK for python3.
6. Now we have to install httpd webserver which is commonly known as apache webserver.
7. Before creating the container we have to copy our simple html code into the folder. For these I create rh.html file in /task_1 folder.Then copy the html file into the managed node .For these we require file and copy modules.
Let's see our file is created or not.It's created.
8. Now we create one httpd container.Then to see the content of the html file that we put inside the container in /ansible_1:/usr/local/apache2/htdocs and need to expose the container on port 80 .For these task we use docker_container module .
Let's see our container is created or not and httpd image is there or not.It's there.
#docker ps
9.To run the whole task that we code in ansible playbook ,run the command -
#ansible-playbook task1.yml
10. Now we are ready to connect the webserver we configured in httpd docker container.
It's working🥳🥳
First Task of Ansible is Successfully Completed.
For any queries or suggestions DM me.
Thank You Mr. Vimal Daga Sir for giving such a task which upskills my ansible knowledge.
$$ Thank you for visiting my article $$
Good going buddy 🤩✨✨
Excellent work ✨