Integrating Ansible with Docker

Devesh Chitlangia
6 min readOct 25, 2020

--

What is Ansible ?

Advantages of Ansible

  • Free : Ansible is an open-source tool.
  • Very simple to set up and use : No special coding skills are necessary to use Ansible’s playbooks (more on playbooks later).
  • Powerful : Ansible lets you model even highly complex IT workflows.
  • Flexible : You can orchestrate the entire application environment no matter where it’s deployed. You can also customize it based on your needs.
  • Agentless : You don’t need to install any other software or firewall ports on the client systems you want to automate. You also don’t have to set up a separate management structure.
  • Efficient : Because you don’t need to install any extra software, there’s more room for application resources on your server.

Ansible’s Features and Capabilities

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

Traditional Automation Approach :

  • It uses imperative programming languages such as python,perl which handles following 1. What to do ? 2. How to do ? — For handling this the script is not intelligent enough to know the commands which needs to be run on different O.S. eg to install firefox on Ubuntu/Rhel8 the command is not known to programming language this 2nd part is effectively handled using Intelligent Automation approach.

Intelligent Automation approach :

  • This uses declarative language & is independent of the O.S. configuration on which it needs to be run. It’s intelligent enough because it knows how to do the configuration/installation of softwares on other O.S. platforms. e.g. Ansible.
  • Ansible uses inventory which is list of IP addresses of managed nodes. By default Config file name is ansible.cfg , its stored under /etc. Pip does not create config file during Ansible installation.

TERMS TO BE REMEMBERED : -

  1. Controller node :- The system on which we run the code of ansible is known as Controller node.
  2. Managed Node :- These nodes are managed by controller node on which Ansible performs configuration (software installation,any other config changes).
  3. Inventory :- This is a database which stores IP of managed nodes which is required by Ansible config file.
  4. Playbooks :- 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.

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 :

Web page launched finally !!

github link :- https://github.com/DeveshChitlangia/Ansible-Task-1/tree/main

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

#Happylearning #sharing #ansible #automation #righteducation

--

--