Ansible- Docker Integration

Abhiroop Bas
2 min readAug 4, 2020
Ansible

What is Ansible?

Ansible is an opensource automation tool used for configuration management and application deployment. It is used for configuring and deploying packages at a system remotely with the use of SSH and SCP protocols.

Ansible is an intelligent tool that uses RAL (Resource Abstraction Layer) to use a common set of commands to deploy packages and actions in various OS without having to tell the exact commands or the OS. Ansible gets this intelligence from modules which enable it to do so.

What are playbooks?

Playbooks are a set of commands written together in a programming file using the YML language and is used to deploy any action using Ansible. It also uses integrations of Jinja to define variables in the script.

Pre-requisites:

  1. RedHat Linux 8 (RHEL 8)
  2. Yum repository setup and configuration
  3. Epel repository setup and configuration
  4. Ansible downloaded
  5. Web page created as home.html

Process:

Ansible can also be used to set up docker and deploy an Apache web server through it. The following playbook demonstrate the action.

- hosts: all

- tasks:

- yum_repository:

name: docker

description: docker yum installation

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

gpgcheck: no

- package:

name: “docker-ce-18.09.1–3.e17.x86_64"

state: present

- pip:

name: docker-py

state: present

- service:

name: “docker”

state: started

register: a

- docker_image:

name: “httpd”

source: pull

- copy:

src: ”home.html”

dest: “/root/html”

- docker_container:

name: “webserver”

image: ”httpd”

state: present

exposed_ports:

- 80/tcp

- 80/udp

published_ports:

- 0.0.0.0:8086:80/tcp

- 0.0.0.0:8086:80/udp

volumes:

- /root/html/:/usr/local/apache2/htdocs

- debug:

var:a

All the plays being performed in about a minute.

Now we may go to the managed node OS and find the web server perfectly hosted by using a Centos:7 inside a docker container. This web page may be visited through any browser connected to the same network using the IP address and the port number.

The above project has been made on RHEL 8.

--

--