Integrating Ansible with Docker

4 min readOct 26, 2020


What is Ansible ?

Ansible’s Features and Capabilities

  • Configuration Management.
  • Application Deployment.
  • Orchestration.
  • Security and Compliance.
  • Cloud Provisioning.


Task Description :-

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

Steps :-

  1. First thing you need to do is that if you don’t have ansible installed , Install it in the controller node. By default ansible is not provided by the Redhat dvd so we can’t install through yum . Ansible is completely created by Python. So we will use the below command to install ansible :
pip3 install ansible

2. Check whether ansible is installed or not using command : -

ansible --version
  • we have to make an inventory so that our controller node comes to know where you wanna perform the tasks.
  • 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

3. Now we have to write an ansible playbook for configuration of Docker.

  • For installation of docker , first we have to configure yum .
- hosts: all
- name: Add docker repo
name: docker
description: docker repo
gpgcheck: no
  • Now we have to install all the docker packages using yum.
- name: Installing Docker packages
name: "docker-ce-18.09.1*"
state: present
register: a- name: Docker installation status
var: a
  • We also need docker-py package to use Ansible docker module.
- name: Installing Docker SDK
name: "docker"
register: b- name: Checking SDK install status
var: b
  • Now start the docker services to use it .
- name: Starting Docker services
name: "docker"
state: started
enabled: yes- command: "systemctl status docker"
register: c- name: Checking the status of Docker service
var: c
  • Next we need to create one html file & also create a separate directory and copy the html file in the managed node.
- name: Creating directory for copying HTML file
path: "/ansible_task1"
state: directory
- name: Copying local file into the directory
src: "home.html"
dest: "/ansible_task1/"
register: d
- name: Status of the local file
var: d

4. Our final step is to run the container using HTTPD image. The image will be automatically downloaded from We have also exposed the port for our container to run our website at the port mentioned & mounted the webpage directory to the /usr/local/apache2/htdocs/ folder.

- name: Creating a container using HTTPD Image
name: myweb
image: httpd
state: started
- "80"
- "8081:80"
- /ansible_task1:/usr/local/apache2/htdocs/
tty: true
detach: true - command: "docker ps"
register: e- name: Status of the Container
var: e

Ansible Playbook execution :-

Managed Node after Docker Configuration :

Thanks, hope you guys will like my article. if you have any suggestion or query will free to ask.

#Happylearning #keepsharing #ansible #automation