Today we are going to integrate the two most popular DevOps tools. Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning. Automation is crucial these days, with IT environments that are too complex and often need to scale too quickly for system administrators and developers to keep up if they had to do everything manually. Automation simplifies complex tasks, not just making developers’ jobs more manageable but allowing them to focus attention on other tasks that add value to an organization. In other words, it frees up time and increases efficiency. And Ansible, as noted above, is rapidly rising to the top in the world of automation tools.


Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, you can quickly deploy and scale applications into any environment and know your code will run.

So We are going to create one playbook of ansible which performs following tasks on Ec2 instance( I am using RedHat 8).

🔅 Configure yum for Docker

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

*** Our first step is to configure yum for Docker because Docker does not directly support on RedHat version 8 ***

name: Docker #This unique id for repo
file: test #This file name followed by extension .repo
gpgcheck: 0
description: This yum repo configure by ansible

Here I am using yum_repository as module of ansible.

***Second step is to Install and configure the Docker***

name: docker-ce-3:18.09.1–3.el7.x86_64
state: present
— name: Starting the Docker service
name: docker
state: started
— name: Installing pip3
name: python3-pip
state: present
— name: Installing Docker sdk
name: docker-py
executable: pip3
state: latest

Here I am using package module of ansible for installing Docker.

***Pulling the Httpd Image from DockerHub***

name: Pulling the httpd image
name: httpd
source: pull

***Running Docker container and exposed it to public and copy the source code also to Docker volumes***

  • name: Creating the docker volume
    name: my_httpd
    — name: Copy Source code into volumes
    src: index.html
    dest: /var/lib/docker/volumes/my_httpd/_data/
    — name: Creating httpd container
    name: myhttpd
    image: httpd
    — “80:80”
    — “my_httpd:/usr/local/apache2/htdocs/:rw”
    detach: true
    restart_policy: always

So In this way we can Integrate Docker with Ansible.

keep Learning follow me on LinkedIn for more such articles