This article is a demonstration of Automation using ANSIBLE

→Ansible is an infrastructure automation tool from REDHAT. It is widely used in the configuration of systems and setting up deployment environments.

→Ansible is an abstraction layer that covers all operating systems under its umbrella that helps configuration of large heterogeneous environment. It is built on top of PYTHON 🐍.

→Ansible has Modules that enables performing various tasks in the system.Power⚡ of Ansible is Playbooks. Playbooks are nothing but YML files containing modules as per user requirements.

Problem Statement For This Hands-On-

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

🔹 Copy the html code in /var/www/html directory and start the web server

🔹 Run the httpd container and expose it to the public

→The system from which user is operating Ansible is called Controller Node and all working nodes under controller node are called Managed Nodes.

→Here I have taken only one Managed node. The file containing information about managed node is called Inventory.

- hosts: DockerSlave
- secret.yml //will discuss about secret later in this article
Managed node and inventory file

→Mention inventory file in Ansible configuration file

  • >Configure yum repository for docker in slave system
- name: yum configuration in slave system
name: DockerRepo
description: docker repo
enabled: true
gpgcheck: no
- name: Docker installation
command: "yum install docker-ce --nobest -y"
Yum repo configured in slave node
  • Install Docker🐋 and start the services.
  • As Ansible is built on top of Python, it is required to download docker package for python.
  • Ansible command and service modules are used in this step.
- name: Docker installation
command: "yum install docker-ce --nobest -y"
- name: Start docker services
name: "docker"
state: started
enabled: yes
- name: Install python36 package
name: python36
state: present
- name: Install python library for docker
name: docker-py
Docker installed
Docker services started

→Here, Httpd image for web server is pulled from DockerHub 🐋

- name: Pull docker image
name: httpd:latest
source: pull

→In this step, using git module, cloned the code from GitHub. One of the ways is to pass the credentials in the link to get authenticated in GitHub. But, it’s not a good practice to pass as plain text in the playbook😕.

→Use concept of Vault to encrypt the secret information and pass it in playbook as parameters. I have created one Vault that contains my GitHub credentials.

Encrypted vault🤗
- name: Clone code from GitHub
repo: ''">https://{{gituser}}:{{gitpass}}'
dest: "/root/code_html/"
  • >This will clone the repository in the destination folder of slave system.
Code cloned in slave node
  • The last step will launch a docker container and expose the port for the public world.
  • Docker_container module is used here to launch and manage the container.
  • The HTML code from local system will be copied in the document root of web server.
- name: Launch container
name: img_httpd
image: httpd:latest
state: started
- "80"
- "2025:80"
- /root/code_html:/usr/local/apache2/htdocs/
Launched container🚀
  • Now, run the following command to apply the playbook in managed node
ansible-playbook <file-name>.yml  --ask-vault-pass 


curl <ip-container>/index.html

I’ll be grateful to have connections like you on Linkedln 🧑‍💼

In a continuous process of Technical Writing. Gathering, Organizing, Crafting the things that make sense.