Creating play-book of Docker by Ansible.
Tags: Ansible, Ansible Playbook, Docker, Httpd, HTML

Introduction

Server automation now plays an essential role in systems administration, due to the disposable nature of modern application environments. Configuration Management tools such as Ansible are typically used to streamline the process of automating server setup by establishing standard procedures for new servers while also reducing human error associated with manual setups.

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.

This article will explains you how to use Ansible to automate the steps contained in our article and How to install and use Docker To deploy web page on Red-Hat 8.

Docker is an application that simplifies the process of managing containers, resource-isolated processes that behave in a similar way to virtual machines, but are more portable, more resource-friendly, and depend more heavily on the host operating system.

Prerequisites

In order to execute the automated setup provided by the playbook we’re discussing in this article, you’ll need:

One Ansible control node: an Red Hat 8 machine with Ansible installed and configured to connect to your Ansible hosts using SSH keys. Make sure the control node has a regular user with sudo permissions and a firewall enabled.

One or more Ansible Hosts: one or more remote Red Hat 8 servers previously set up, these will be work as a managed node.

Step 1 : Installing & Configuring Ansible

→ Installation of Ansible :

We can install docker manual or automation, for automation we need 2 or more OS. In which OS we will write the automation code it will known as Controller Node & other will known as Manage Node.

On Controller Node first we have to install Ansible :

i.e :- Here, I am using Redhat linux so for installing Ansible in Redhat linux

→ pip3 install ansible

To check the version of ansible

→ ansible — version

→ Create an Inventory :

We have to create a file with extension “.txt”

i.e. :- Creating a file in Redhat linux by using vim text-editor

→ vim ip.txt

In this file we have to store IP of all the Manage-node.

Syntax :-

I P (of manage node) ansible_user = (which you have given to OS) ansible_ssh_pass=(Password of OS) ansible_connection=ssh

→ Configure the Ansible

Now, We have to tell Ansible that we have inventory to use for this we have to configure Ansible

→ Create a directory

→ mkdir /etc/ansible

→ Create a file in directory

→ vim /etc/ansible/ansible.cfg

In this file we have to update the these

Syntax :

[defaults] inventory=/root/ip.txt

→ ansible --version

It will give the ansible the location of inventory.

→TO check the how many Manage-node has connected :

→ ansible all --list-hosts

Step 2 : Start Docker service’s on Target or Manage Node

Ansible playbook code

What Does this Playbook Do?

An Ansible playbook is an organized unit of scripts that defines work for a server configuration managed by the automation tool Ansible.

Running this playbook will perform the following actions on our Ansible hosts:

→ Configure yum for docker , Add the official Docker repository to the yum repository

- hosts: web

tasks:

- name: Add Docker Repository

yum_repository:

name: docker

description: this is docker repo

baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/

gpgcheck: no

→ Install Docker

- name: Install Docker

package:

name: "docker-ce-18.09.1-3.el7.x86_64"

state: present

→Start Docker service

- name: docker service start

service:

name: "docker"

state: started

enabled: yes

→Install the Python Docker module via pip.

- pip:

name: docker-py

→Copy file which contain code that we have to deploy on the webserver.

- copy:

src: "index.html"

dest: "/var/www/html"

“index.html” contains simple html code.

→ Pull the httpd image from Docker Hub.

Create the containers using httpd image , expose it to the out site world connectivity & deploy the developer code on this container.

- name: create a container

docker_container:

name: docos

image: httpd

state: started

ports:

- "80"

volumes:

- /var/www/html:/usr/local/apache2/htdocs/

How to Use this Playbook

The playbook file, containing the tasks to be executed on the remote server.

Run command

→ ansible-playbook <play book name>

i.e. :- I have created a playbook “avi.yml” to run this the syntax will be

→ansible-playbook avi.yml

When the playbook is finished running, log in via SSH to the server provisioned by Ansible.

by “docker ps “ we can check the detail of conatiner

Now we can access our web page. using Ip of managed node and port no of container.