Infrastructure development With Ansible and Docker

Infrastructure development With Ansible and Docker

Nowadays Automation is everywhere

Ansible and Docker are the most important tools in DevOPs and they are playing a major role in automating and setting up Infrastructure with just one click and setting up the platform within seconds

Ansible:

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

The most important things about Ansible are

It is Agentless and Serviceless and it logins remoteley using SSH

Unlike Puppet, We don't require to always start the services for Ansible, as it is serviceless. It can run with just 1-2gb RAM.Any machine with Ansible utilities installed can leverage a set of files/directories to orchestrate other nodes. The absence of a central-server requirement greatly simplifies disaster-recovery planning. Nodes are managed by this controlling machine - typically over SSH. The controlling machine describes the location of nodes through its inventory.

With Anisble installed on the Controller Node, we can control and setup the infrastructure irrespective of the OS type and different commands.

Ansible is automated using Ansible Playbooks. Ansible Playbooks are just a script written using Declarative language i.e. using YAML language. Playbooks express configurations, deployment, and orchestration in Ansible and allow Ansible to perform operations on managed nodes. Each Playbook maps a group of hosts to a set of roles. Each role is represented by calls to Ansible tasks.

Docker:

Docker is a set of Platform as a Service (PaaS) product that use OS level virtualization to deliver software in packages called containers. It is used to launch a OS with minimum packages or software and hence it just few 100 MB of RAM. Best thing about docker is takes the required RAM only and it automatically adjusts and shares the resources from the host. It is used to deploy applications using the containers. We can launch the containers using the docker images same as the ISO image which we use to install an OS on Virtualization or Bare Metal. Docker Containers are isolated from one another and they come with their own software bundle, libraries and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single OS and therefore use fewer resources than VMs

So lets jump to our Problem Statement

?? *Task 1* ??

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

?? Run the httpd container and expose it to the public

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

So we have to write a Ansible Playbook for the above task

But first of all we have to configure DNS server and Yum for the above task

We can do it manually but if there are many systems to be managed then it would be tedious task. Hence I will include the script for configuring them in the playbook.

For this task we will Integrate Ansible with Docker

Step1: Configuring Ansible

After installing Ansible we have to give the IP of the nodes which we want to manage.

Create a File with "myhosts.txt" and include the code given below.

**Don't forget to change the IP and username and password

[web]
192.168.29.189 ansible_ssh_user=root ansible_ssh_pass=redhat


No alt text provided for this image

Then create a folder called "ansible" in /etc/ directory

mkdir /etc/ansible

Then create a file called "ansible.cfg" in that directory

vim /etc/ansible/ansible.cfg

Include the below code in it

[defaults]
inventory = /etc/myhosts.txt
host_key_checking = False

No alt text provided for this image

To check run the below commands

We should see the config file location and the no. of hosts

ansible --version
ansible all --list-hosts
No alt text provided for this image

Hence Ansible is configured

Before starting create these two files.

Use this Github Repo: https://github.com/Syed-Faheemuddin/Ansible_Task1.git

No alt text provided for this image

Lets jump to code of Ansible File

- hosts: web
  vars_prompt:
  - name: dnsIP
    prompt: "Enter ur DNS IP"
    private: no

This code implies that the ansible should go to the group of clients which are configured as "web" i.e to hosts the webpages. And we will configure DNS server as well hence we will prompt the user to give DNS name server IP

***The rest of the below code will come under tasks section

Configuring DNS server

  tasks:
  - name: Configuring DNS Server
    template:
      src: dns.conf.j2
      dest: /etc/resolv.conf
    register: x
  - debug:
      var: x.failed


The above is the beginning of the task section. This task is to configure DNS server on the client. The IP given by the user will set as DNS nameserver IP and it gets replaced in the "dns.cfg.j2" file will running .In the debug section we print whether the task is failed is or not

Configuring Yum by adding the Docker repo

  - name: Adding Docker repository
    yum_repository:
      name: docker
      description: Docker repo
      baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/
      gpgcheck: no


This code will configure the yum and it will help in installing the compatible Docker version suitable for the client. We give gpgcheck=0 which means there should any checking for security etc

Installing Docker

  - name: Installing Docker
    package:
      name: "docker-ce-18.06.3.ce-3.el7.x86_64"
      state: present


This code will help in downloading and installing Docker

Installing Python3

  - name: Installing Python3
    package:
      name: "python3"
      state: present


This will install python3

Creating a workspace in the Client system

  - name: Creating the Workspace
    file:
      path: /root/Ans_task1
      state: directory


This will create a directory named "Ans_task1" in the given path

Copying the HTML into the Client system

  - name: Copying the HTML page
    copy:
      src: "web1435.html"
      dest: "/root/Ans_task1/"
    ignore_errors: yes

THis code will copy the HTML file from the controller node to the worker node to the required detination

Starting and Enabling the Docker services

  - name: Starting Docker Services
    service:
      name: "docker"
      state: started

  - name: Enabling service Docker
    service:
      name: docker
      enabled: yes

This code will start and enable and Docker services

Shorter code:

  - name: Starting Docker Services
    service:
      name: "docker"
      state: started
      enabled: yes

Installing docker-py using pip

  - pip:
      name: docker-py

Pulling HTTPD image

  - name: pulling an image
    docker_image:
      name: httpd
      source: pull

Starting a Container

  - name: Starting a container
    docker_container:
      name: WebServer
      image: httpd
      state: started
      ports:
      - "1456:80"
      volumes:
      - /root/Ans_task1/:/usr/local/apache2/htdocs/
  - debug:
      msg: "Go to port number 1456"

This code will launch a container from HTTPD image and mount it with a volume and as well exposes the container to the outside world. And we print a message specifying the port number

With that the code for the playbook is completed

The total code will look like

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

To Run the Playbook

ansible-playbook DocAns1.yml
No alt text provided for this image
No alt text provided for this image


In the Client system before running the Playbook

No alt text provided for this image

After running the playbook

No alt text provided for this image

The webpage is

No alt text provided for this image

Hence we successfully deployed a webpage just in one click by integrating Ansible with Docker

Thank You very much

Any Queries Feel free to ask

Author : Syed Faheemuddin

University of Hyderabad

LinkedIN :

Github:


Mentor: Vimal Daga

Juhi Gupta

Solution Architect

4 年

Nice ??

Moteeullah Azmi

Full Stack Developer | Next.js | React.js | TypeScript

4 年

Nic

Vimal Daga

World Record Holder | 2x TEDx Speaker | Philanthropist | Sr. Principal Consultant | Entrepreneur | Founder LW Informatics | Founder Hash13 pvt ltd | Founder IIEC

4 年

Grt

要查看或添加评论,请登录

Syed Faheemuddin的更多文章

  • AWS CLI TASK2

    AWS CLI TASK2

    In this article I will integrate various services of AWS : EC2, EBS, S3 and Cloud Front I will launch a webserver using…

  • Transfer Learning using MobileNet

    Transfer Learning using MobileNet

    What is Transfer Learning? Transfer learning is the reuse of a pre-trained model on a new problem. It's currently very…

  • Getting Started with AWS CLI

    Getting Started with AWS CLI

    Have you ever imagined if you had to run 100s of instances at a time to solve a particular use-case. We all know much…

  • Automation using pipeline in Jenkins Image integrating with Docker and GitHub

    Automation using pipeline in Jenkins Image integrating with Docker and GitHub

    In this task I will be developing a CI/CD pipeline which downloads the code form GitHub and deploys on respective…

  • Jenkins Testing and Production Environment with Automation

    Jenkins Testing and Production Environment with Automation

    TASK 1 --> MLOPS and DevOps Assembly Lines In this task I will explain a SMARTER and AGILE way to manage testing and…

  • Integrating ML with DevOps

    Integrating ML with DevOps

    In this article I will explain my first MLOPS project which successfully completed under Vimal Daga Sir. In this…

社区洞察

其他会员也浏览了