Integration of Ansible with Docker

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 .

No alt text provided for this image

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.

No alt text provided for this image

2. For the target node we can launch one managed node.

No alt text provided for this image

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.

No alt text provided for this image

To see all target nodes run command -

#ansible all --list-hosts

No alt text provided for this image

To see all nodes are connected or ping run command -

#ansible all -m ping

No alt text provided for this image

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.

No alt text provided for this image

Let's see our repository add or not in managed node.It's added.

No alt text provided for this image

4.Then we have to install docker package having stable version using command and to start and enable the docker services using service module.

No alt text provided for this image

5. To use ansible docker module we need to install docker SDK for python3.

No alt text provided for this image

6. Now we have to install httpd webserver which is commonly known as apache webserver.

No alt text provided for this image

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.

No alt text provided for this image

Let's see our file is created or not.It's created.

No alt text provided for this image

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 .

No alt text provided for this image

Let's see our container is created or not and httpd image is there or not.It's there.

#docker ps

No alt text provided for this image

9.To run the whole task that we code in ansible playbook ,run the command -

#ansible-playbook task1.yml

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

10. Now we are ready to connect the webserver we configured in httpd docker container.

No alt text provided for this image

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 $$









To view or add a comment, sign in

More articles by Simran Kukareja

Others also viewed

Explore content categories