Docker Setup Using Ansible!!

Rahul Prajapati
4 min readAug 4, 2020

Ansible

Ansible is a radically simple IT automation system. It handles configuration management, application deployment, cloud provisioning, ad-hoc task execution, network automation, and multi-node orchestration. Ansible makes complex changes like zero-downtime rolling updates with load balancers easy. It is being operated push mechanism.

Ansible terminologies:

Inventory-

A file (by default, Ansible uses a simple INI format) that describes Hosts and Groups in Ansible. Inventory can also be provided via an Inventory Script (sometimes called an “External Inventory Script”).

Jinja2-

Jinja2 is the preferred templating language of Ansible’s template module. It is a very simple Python template language that is generally readable and easy to write.

Push Mode-

Push mode is the default mode of Ansible. In fact, it’s not really a mode at all it’s just how Ansible works when you aren’t thinking about it. Push mode allows Ansible to be fine-grained and conduct nodes through complex orchestration processes without waiting for them to check in.

Task-

Playbooks exist to run tasks. Tasks combine an action (a module and its arguments) with a name and optionally some other keywords (like looping directives). Handlers are also tasks, but they are a special kind of task that do not run unless they are notified by name when a task reports an underlying change on a remote system.

Tasks-

A list of Task.

Templates

Ansible can easily transfer files to remote systems but often it is desirable to substitute variables in other files. Variables may come from the inventory file, Host Vars, Group Vars, or Facts. Templates use the Jinja2 template engine and can also include logical constructs like loops and if statements.

Docker

Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security allow you to run many containers simultaneously on a given host. … You can even run Docker containers within host machines that are actually virtual machines!

Description:-

>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 webserver.

Approach:-

To install ansible in RedHat linux use following command.

pip3 install ansible 
To check the version of ansible

Now we have to create the file for Inventories(managed nodes) from where we will keep the IPs and access permission for many hosts nodes.

After creating file for inventories now we need to create the ansible configuration file mentioning that path of inventories and giving permission to control node to do SSH in all the managed nodes.

First check the connectivity between Controller node and Managed node by ping command , if the connection are not stable then ansible-playbook will not configure and it will give error.

Now we have to create the configuration file for installing docker and running further services , Ansible uses Yaml language for configuring and for that file will be created in .yml extension i.e. called playbook.

Creating Playbook for Operation

This will install the docker and it will also download the httpd image from docker.hub
By using ansible we will copy the host file into the managed node’s directory where we gonna launch the server using docker container
This will launch the container on port 8686

We have to just give a single command to run all the tasks given above. This is the power of ansible.

ansible-playbook docker.yml

The playbook ran successfully and it configured every tasks.

This command will let you know about docker status
The container launched successfully

To check the web page is being launched successfully on the container or not.

to check in cli

--

--