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
- secret.yml //will discuss about secret later in this article
→Mention inventory file in Ansible configuration file
- >Configure yum repository for docker in slave system
- name: yum configuration in slave system
description: docker repo
gpgcheck: no- name: Docker installation
command: "yum install docker-ce --nobest -y"
- 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
enabled: yes- name: Install python36 package
state: present- name: Install python library for docker
→Here, Httpd image for web server is pulled from DockerHub 🐋
- name: Pull docker image
→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.
- name: Clone code from GitHub
- >This will clone the repository in the destination folder of slave system.
- 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
- Now, run the following command to apply the playbook in managed node
ansible-playbook <file-name>.yml --ask-vault-pass
Thank you guys for taking the time out to read my rant lol!🥰😅
I’ll be grateful to have connections like you on Linkedln 🧑💼