LINUX AUTOMATION WITH ANSIBLE

Poojan Mehta
4 min readSep 5, 2020

This article is a demonstration of Automation using ANSIBLE

~WHAT IS 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
vars_files:
- secret.yml //will discuss about secret later in this article
Managed node and inventory file

→Mention inventory file in Ansible configuration file

STEP -1)

  • >Configure yum repository for docker in slave system
- name: yum configuration in slave system
yum_repository:
name: DockerRepo
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/
description: docker repo
enabled: true
gpgcheck: no
- name: Docker installation
command: "yum install docker-ce --nobest -y"
Yum repo configured in slave node

→STEP-2)

  • 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
service:
name: "docker"
state: started
enabled: yes
- name: Install python36 package
package:
name: python36
state: present
- name: Install python library for docker
pip:
name: docker-py
Docker installed
Docker services started

→STEP-3)

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

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

→STEP-4)

→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
git:
repo: '@github.com/poojan1812/hybrid-cloud.git'">https://{{gituser}}:{{gitpass}}@github.com/poojan1812/hybrid-cloud.git'
dest: "/root/code_html/"
  • >This will clone the repository in the destination folder of slave system.
Code cloned in slave node

→STEP-5)

  • 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
docker_container:
name: img_httpd
image: httpd:latest
state: started
exposed_ports:
- "80"
ports:
- "2025:80"
volumes:
- /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 

DONE🤩🤩🤩

FINAL OUTPUT-

curl <ip-container>/index.html

Thank you guys for taking the time out to read my rant lol!🥰😅

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

--

--

Poojan Mehta

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