Integrating Ansible with Docker

Abhishekkumawat
4 min readOct 26, 2020

What is Ansible ?

Ansible’s Features and Capabilities

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

CONTROLLER NODE & MANAGED NODE ARCHIETECTURE :-

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
tasks:
- name: Add docker repo
yum_repository:
name: docker
description: docker repo
baseurl:
https://download.docker.com/linux/centos/7/x86_64/stable/
gpgcheck: no
  • Now we have to install all the docker packages using yum.
- name: Installing Docker packages
package:
name: "docker-ce-18.09.1*"
state: present
register: a- name: Docker installation status
debug:
var: a
  • We also need docker-py package to use Ansible docker module.
- name: Installing Docker SDK
pip:
name: "docker"
register: b- name: Checking SDK install status
debug:
var: b
  • Now start the docker services to use it .
- name: Starting Docker services
service:
name: "docker"
state: started
enabled: yes- command: "systemctl status docker"
register: c- name: Checking the status of Docker service
debug:
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
file:
path: "/ansible_task1"
state: directory
- name: Copying local file into the directory
copy:
src: "home.html"
dest: "/ansible_task1/"
register: d
- name: Status of the local file
debug:
var: d

4. Our final step is to run the container using HTTPD image. The image will be automatically downloaded from hub.docker.com. 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
docker_container:
name: myweb
image: httpd
state: started
exposed_ports:
- "80"
ports:
- "8081:80"
volumes:
- /ansible_task1:/usr/local/apache2/htdocs/
tty: true
detach: true - command: "docker ps"
register: e- name: Status of the Container
debug:
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

--

--