Automate Docker With Ansible !

Automate Docker With Ansible !

What is Ansible?

Ansible is an Open-Source IT Automation Engine that is used to automate application deployment, infrastructure automation, cloud provisioning, and many IT services. Ansible improves the scalability, consistency & reliability of the IT environment.

Ansible is easy-to deploy as it is agentless and doesn't require any security infrastructure. Ansible uses the concept of a playbook and uses a language. YAML (Yet Another Markup Language) for its configuration management. Its advantage is that playbooks are so simple that even the support guys can debug it.

Basic terms in ansible

Controller node: Any machine with Ansible installed. You can run Ansible commands and playbooks by invoking the ansible or ansible-playbook command from any control node. You can use any computer that has a Python installation as a control node - laptops, shared desktops, and servers can all run Ansible. However, you cannot use a Windows machine as a control node. You can have multiple control nodes.

Managed Node: The network devices (and/or servers) you manage with Ansible. Managed nodes are also sometimes called “hosts”. Ansible is not installed on managed nodes.

Inventory: A list of managed nodes. An inventory file is also sometimes called a “hostile”. Your inventory can specify information like the IP address for each managed node. An inventory can also organize managed nodes, creating and nesting groups for easier scaling.

Playbooks: Ordered lists of tasks, saved so you can run those tasks in that order repeatedly. Playbooks can include variables as well as tasks. Playbooks are written in YAML and are easy to read, write, share, and understand.

What is Docker?

Docker is an OpenSource containerization platform for Building, Shipping, and Running applications using Container Virtualization technology. Docker packages all the dependencies in form of Docker containers to ensure that our application works seamlessly in any of the environments.

If you are not familiar with Docker and have not installed Docker on your device, you can go through my last article mentioning the installation here.

In this article, I will show you how to Automate Docker with Ansible. We will Automate various Operations in the managed Node.

For Automation, we need to write a PlayBook for the following operation given below.

  • 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 webserver

PlayBook for Entire Docker Setup

This Playbook you need to write in your Controller Node

- hosts: all
  tasks:
          - name: creating a docker repository
            yum_repository:
                 description: "repo for docker"
                 name: "docker-ce"
                 baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable/"
                 gpgcheck: "no"

          - name: installing docker
            package:
                  name: "docker-ce-18.09.1-3.el7.x86_64"
                  state: present

          - name: starting docker service
            service:
                   name: docker
                   state: started
          - pip:
                name: docker-py

          - name: pulling docker image
            docker_image:
                   name: httpd
                   source: pull

          - name: Copying file into home
            copy:
                 src: /root/ws1/index.html
                 dest: /home

          - name: Create a data container
            community.general.docker_container:
                      name: sarthak
                      image: httpd
                      exposed_ports:
                        - 80
                      ports:
                        - 4444:80
                      state: started
                      detach: yes
                      volumes: /home/:/usr/local/apache2/htdocs/


In the above playbook, we used different modules for each task which are the actual intelligence behind the ansible to make our things easier.

Check your Managed Node that it has Docker Install or not

No alt text provided for this image

Run the PlayBook

$ ansible-playbook docker.yml

No alt text provided for this image
  • Now the Docker is Successully installed in our other System.
# rpm -q docker-ce
No alt text provided for this image
  • httpd server image is successfully pull from the Docker Hub
# docker ps -a
No alt text provided for this image
  • Web Server is also working fine
# curl [ip address]:[port no]


No alt text provided for this image

This is all for this article, all the above Operation had proved that ansible is one of the powerful tools for Automation.



要查看或添加评论,请登录

社区洞察

其他会员也浏览了