Integration Of Ansible With Docker

Kunal Jaiswal
4 min readAug 18, 2020

--

What is Ansible?

Ansible is an open source IT Configuration Management, Deployment & Orchestration tool. It aims to provide large productivity gains to a wide variety of automation challenges. This tool is very simple to use yet powerful enough to automate complex multi-tier IT application environments.

Advantage of Ansible

1.) Free : Ansible is an open-source tool.

2.) Very simple to set up and use : No special coding skills are necessary to use Ansible’s playbooks (more on playbooks later).

3.) Powerful : Ansible lets you model even highly complex IT workflows.

4.) Flexible : You can orchestrate the entire application environment no matter where it’s deployed. You can also customize it based on your needs.

5.) 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.

6.) Efficient : Because you don’t need to install any extra software, there’s more room for application resources on your server.

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.

Task which we are going to preform

1.) Configure yum for the docker

2.) Installing docker software

3.) Start the docker service

4.) Installing python36 software

5.) Installing a docker-py package

6.) Pull the httpd for docker

7.) Copy the file into the /var/www/html directory

8.) Run the httpd server and exposed it to public and also copy a file into it

Let’s do some practical

Firstly we make a yml file to perform the ansible task

gedit dockerhttpd.yml

Then, we are going to give the host-name where we are going to setup all the things

- hosts: dockerhost

Step 1: Configure yum for the docker

- name: conf yum for the docker
yum_repository:
name: dockerrepo
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/
description: yum for the docker
gpgcheck: no

Step 2: Installing docker software

- name: install docker software
command: "yum install docker-ce -y --nobest"

Step 3: Start the docker service

- name: start docker service
service:
name: "docker"
state: started
enabled: yes

Step 4: Installing python36 software because ansible works on the pip command and pip command is provide form the python36

- name: install software python36
package:
name: "python36"
state: present

Step 5: Installing docker-py package because through this package only pull command work.

- name: install docker python library
pip:
name: docker-py

Step 6: Pull the httpd for docker

- name: pull httpd for docker
docker_image:
name: "httpd"
source: pull

Step 7: Copy the file into the /var/www/html directory

- name: copy the file
copy:
src: "docker.html"
dest: "/var/www/html/"

Step 8: Run the httpd and exposed it and also mounted the webpage directory to the /usr/local/apache2/htdocs/ folder.

- name: run httpd
docker_container:
name: myweb
image: httpd
state: started
exposed_ports:
- "80"
ports:
- "8081:80"
volumes:
- /var/www/html:/usr/local/apache2/htdocs/
tty: true
detach: true

Now, run the yml file and see the result on the host (Managed Node)

For run the file give the command in the Controller node

ansible-playbook dockerhttpd.yml
Yum is successfully configure.
Docker software successfully installed.
Docker is in running.
Python36 is successfully installed and pip3 command is properly work.
docker-py package is successfully installed.
httpd is in running condition.
This shows httpd is exposed in public.

So, our task is successfully done✌✌

File link https://github.com/KunalKumarJaiswal/Ansible/blob/master/dockerhttpd.yml

Thanks for learning. If you have any suggestion or any query then feel free for suggest and ask.

--

--