Integration of Ansible & Docker In RHEL 7 & 8

Ayush Kumar singh
4 min readAug 22, 2020

--

I’ll Show How One Can Install Docker and Configure an Apache Web Server in all Nodes Using Ansible Playbooks.

What We’ll Do?

  • Configure Docker
  • Pull The HTTPD Server Image From Docker Hub
  • Run The HTTPD container and Expose It To The Public
  • Copy The HTML Code In and Start The WebServer

Everything Will Be Performed Using Ansible-Playbook.

So What Is Ansible ??

Ansible is an intelligent tool for automation, to be specific, configuration management, application deployment, cloud provisioning and many other IT needs.

Ansible supports one to many connections and automates tasks at a time. For instance, if we have 100 servers with the heterogeneous environment, using Ansible we have to tell it “what to do?” and “where to do?”, here we can provide it with a list of remote servers doesn’t matter how much. Ansible, with its intelligence, will do the desired task, that means, it automatically finds “how to do it?”.

Installing Ansible :

Ansible is Developed on Python so it One of the Pre-requisite. And We’ll be using Python pip For Installing Ansible.

Use: pip3 install ansible

Configure Ansible :

Now Lets Start Configuring Ansible. We have to Tell Ansible About All the Nodes Manually, and For that, we’ll create an Inventory which is a file with IP’s of the Managed Node, i.e. The systems Which We are Controlling.

The Format Of Inventory file is as Shown :

Save this as Hosts.txt in /etc/hosts.txt

Now create a directory in “ansible” in /etc. And create a file inside it named Ansible .cfg. In this file, we have to paste the location of the Inventory we created just now. In the Shown Format

To check the Configuration Type: Ansible — version and it will show the location of the config file as shown. By chance it shows none, try to configure it again.

Let’s Start and Configure Docker in All our Nodes

For this, I have my An ansible-playbook just run it in Your Master node and it will setup Docker container running apache server in it.

Ansible-Playbooks are the files where the Ansible code is written. … Playbooks are one of the core features of Ansible and tell Ansible what to execute in the Node.

The Complete PlayBook is available on my GitHub here. : Click_Here

Installation of Docker in Rhel using Ansible

Installation of Docker in Rhel is not Easy…By default RedHat Only Allow the Enterprise Edition of Docker(Paid) but here I installing Docker Community Edition which is free.

For this, We will manually Configure yum Repo and add Docker-CE repo.

Let’s Do it using Ansible in all the Node:

For RHEL 8 we only Need One Repo file, i.e. Docker official Repo but for RHEL 7 we need some Extra Repo.

For Installation Their is Different ways in rhel 8 and rhel 7. So I’ll use a conditional statement For Installation. let’s go

This code will install and start the Docker services in all the Nodes.

Now Docker is Successfully installed. Let’s Install apache in it and start it.

For Controlling Docker Containers from Ansible, we must have a python module installed named ‘docker’. For installing that we’ll use pip package manager.

Then we’ll create a container with apache server and configure it.

Make necessary changes line 70 and 71 as it will copy your Website data to the container.

Now Combine the Codes and Run it using Ansible.

Get the Complete Code Here: Github

For Running The Code Using Ansible

USE: “ansible-playbook docker_Install_config.yml”

>>> 1 2 3! Done!

Thanks For reading Hope it helps You!

Any Suggestion or Query. Ping Me

LinkedIn: Here

--

--

No responses yet