Integrating Ansible with Docker
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.
Ansible’s Features and Capabilities
- Configuration Management.
- Application Deployment.
- 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.
- 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 : -
- 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
description: docker repo
- Now we have to install all the docker packages using yum.
- name: Installing Docker packages
register: a- name: Docker installation status
- We also need docker-py package to use Ansible docker module.
- name: Installing Docker SDK
register: b- name: Checking SDK install status
- Now start the docker services to use it .
- name: Starting Docker services
enabled: yes- command: "systemctl status docker"
register: c- name: Checking the status of Docker service
- 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
- name: Copying local file into the directory
- name: Status of the local file
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
detach: true - command: "docker ps"
register: e- name: Status of the Container