How to integrate Ansible with Docker

Nithish Kumar
4 min readAug 1, 2020

--

🤔 What is 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. It includes its own declarative language to describe system configuration. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015. Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks.

🤔What is Ansible PlayBook ??🤔
Playbooks are YAML files that 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.

📝 Write an Ansible PlayBook that does the following operations in the managed nodes:
👉 Configure Docker & Install it.
👉 Start and enable Docker services.
👉 Pull the httpd server image from the Docker Hub.
👉 Copy the html code to the directory in the container .
👉 Run the httpd container and expose it to the public .

Steps to proceed :
Step 1:
Install Ansible using pip command ….
Here I’m using RedHat v8 .. In this version, Python36 is already installed… Ansible is more compatible with python … Now we have to install ansible on the top of python36 …
Here is the command :

pip3 install ansible

Check whether ansible is installed or not using command …

Step 2: Now configure the ansible …
→ Save the IP address of Managed Node in <file.txt> and also give username and password for login purpose … Here is my file …

→Ansible always checks for .cfg file … So that we have to create one file called ansible.cfg ,.. And in that , we have to write the inventory which we have saved before … Here is ansible.cfg file …

Step 3: Now We have to write the playbook to configure webserver on Docker …
→write the hosts group and first task (i.e) configuring repo for Docker using
yum_repository module …

- name: Configuring Webserver using Docker
hosts: webserver
tasks:
— name: Adding Docker repository
yum_repository:
name: Docker
description: Docker Repo
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable
gpgcheck: no

→Now Installing docker using package module … Here I am using particular version of Docker which is stable …

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

→ Now start and enable the service docker using service module…

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

→To use Ansible Docker module we need to install docker SDK for python3 …
Here is the code …

- name: Installing Docker SDK for python3
command: pip3 install docker

→ Create one html file … And also create a separate directory and copy the html file in the managed node …

- name: Creating folder and copying html file...
file:
path: /webpages
state: directory
- name: Copying html files
copy:
src: "kumar.html"
dest: "/webpages/"

→Final step is to run the container using HTTPD image… The image will automatically downloads from hub.docker.com… And exposed to public to access the webpages … Mounted the pre-created webpages folder to the /usr/local/apache2/htdocs …..Here is the code…

- name: Creating container using HTTPD Image 
docker_container:
name: MyWebServer
image: httpd
state: started
exposed_ports:
— "80"
ports:
— "8888:80"
volumes:
— /webpages:/usr/local/apache2/htdocs/

That’s it,.. Coding part is done ….
Now run the command where ur yml code is saved ..

ansible-playbook task.yml

Output:

As we know the managed IP .. So, browse the “IP:8888/<file.html>”

👏 Done with the task 👏
Finally, Integrated Ansible with Docker to Configure webserver …
Here is the
GitHub Link for code ….
😊 … Thanks for reading … 😊

👋 …. Signing Off …. 👋

--

--

Nithish Kumar
Nithish Kumar

Written by Nithish Kumar

Aspiring DevOps/Cloud Engineer. #Believe in you.

No responses yet