DEV Community

Poojan Mehta
Poojan Mehta

Posted on • Originally published at poojan-mehta.Medium on

LINUX AUTOMATION WITH ANSIBLE

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
Enter fullscreen mode Exit fullscreen mode


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/](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"
Enter fullscreen mode Exit fullscreen mode


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
Enter fullscreen mode Exit fullscreen mode


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
Enter fullscreen mode Exit fullscreen mode

→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](https://{{gituser}}:{{gitpass}}<a%20href=).com/poojan1812/hybrid-cloud.git'">https://{{gituser}}:{{gitpass}}[@github](http://twitter.com/github).com/poojan1812/hybrid-cloud.git'
     dest: "/root/code_html/"
Enter fullscreen mode Exit fullscreen mode
  • >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/
Enter fullscreen mode Exit fullscreen mode


Launched container🚀

  • Now, run the following command to apply the playbook in managed node
ansible-playbook <file-name>.yml --ask-vault-pass 
Enter fullscreen mode Exit fullscreen mode

DONE🤩🤩🤩

FINAL OUTPUT-

curl <ip-container>/index.html
Enter fullscreen mode Exit fullscreen mode

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

I’ll be grateful to have connections like you on Linkedl *n * 🧑‍💼

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more