Automate Docker with Ansible

Automate Docker with Ansible

In this article, we integrate ansible with docker by writing playbook in ansible.

Ansible automates and simplifies repetitive, complex, and tedious operations. Everybody likes it because it brings huge time savings when we install packages or configure large numbers of servers. Its architecture is simple and effective. It works by connecting to your managed nodes and pushing small programs to them by using ssh client.

In this world we know that when we work in a heterogeneous environment we face many difficulties. We don’t know which command that server supports. There are many different types of Operating Systems that have different commands to do operations.

TASK DESCRIPTION:

Write an Ansible Playbook that do following operations in managed node.

  • Configure Docker
  • configure yum repository
  • 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

Ansible:

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment,etc.Ansible is agentless, temporarily connecting remotely via SSH to do its tasks.

Docker:

Docker is the most popular file format for Linux-based container development and deployments. If you’re using containers, you’re most likely familiar with the container-specific toolset of Docker tools that enable you to create and deploy container images to a cloud-based container hosting environment.

In this task we automate ansible with docker using ansible-playbook. We install docker and start services using automation.

Pre-requisite:

1. Two RHEL OS on your system with yum configuration.

  • Controller Node
  • Managed Node

So let’s start for this task.

First we check that python is installed or not in the Controller node. If python is installed then we install ansible. we dont require any ansible or python on managed node.

python3 -V

pip3 install ansible

ansible  --version
No alt text provided for this image

In this above snap we found the ansible current version, python version and ansible configuration file.

Ansible knows about its target node from inventory file that we have to create an inventory and give the path of that inventory file to the ansible configuration file

Configuration file ===>>>   /etc/ansible/ansible.cfg 
No alt text provided for this image

In this, above inventory file we put managed target IP address and password as well and put this inventory entry in the ansible config file

To see all target nodes run command -

ansible all — list-hosts

To see all nodes are connected or ping run command -

ansible all -m ping
No alt text provided for this image

So now let’s start to write code in one ansible-playbook.

We use YAML language to code in ansible playbook . so first we add one docker repository to yum repository in managed node.

$vim docker_play.yml

----------------------------------------------

- name: " Automate Docker with ansible"
  hosts: all
  tasks:
    - name: Adding Docker Repository
      yum_repository:
        name: Docker  
        description: Docker repo
        baseurl: https://download.docker.com/linux/centos/7/x86_64/stable
        gpgcheck: no
        enabled: yes
      register:  a_varible
    - debug:
        var: a_varible  

----------------------------------------------

$ansible-playbook docker_play.yml

Let’s see our repository add or not in the managed node.

No alt text provided for this image

Now we have to install a docker package having a stable version using command and to start and enable the docker services using the service module.

$vim docker.yml

----------------------------------------------

- name: Installing docker package
  command: "yum install docker-ce -y --nobest"
  register: b_varible
- debug:
    var: b_varible

- name: Start docker services 
  services:
    name: "docker"
    state: started
    enabled: yes
  register: c_varible
- debug:
    var: c_varible

-------------------------------------------------

$ansible-playbook docker_play.yml

To use ansible docker module we need to install docker SDK for python3 and we have to install httpd web server which is commonly known as apache webserver.

$vim docker_play.yml

----------------------------------------------

- name : Installing docker SDK
  command: "pip3 install docker"
  register: d_varible
- debug:
    var: d_varible

- name : pull image of httpd webserver
  docker_image:
    name: httpd
    source: pull
  register : e_varible
- debug:
    var: e_varible
------------------------------------------

 Before creating the container we have to copy our simple Html code into the folder. For these, I create a rh.html file in /task_1 folder. Then copy the html file into the managed node. For these, we require file and copy modules.

$vim docker_play.yml

-----------------------------------

- name : creating folder
  file: 
    path: /ansible_dir
    state: directory
  register: f_varible
- debug:
    var: f_varible

- name: copy web page that we have created 
  copy:
    src: /task_1/index.html
    dest: /ansible_dir
  register: g_varible
- debug:
    var: g_varible

-----------------------------------

Now we create one httpd container. Then to see the content of the html file that we put inside the container in /ansible_dir:/usr/local/apache2/htdocs and need to expose the container on port 80. For this task, we use the docker_container module.


$vim docker_play.yml

---------------------------------------

- name: Creating docker container
  docker_container:
    name: "webserver"
    image: httpd
    state: started
    exposed_port: "80"
    ports: "8080:80"
    volumes:
      - /ansible_dir:/usr/local/apache2/htdocs/
  register: h_varible
- debug:
    var: h_varible

---------------------------------------

$ansible-playbook docker_play.yml

Now Let’s see our container is created or not and httpd image is there or not in managed node.

$docker ps
No alt text provided for this image

Here are some artifacts which we have seen during the ansbile playbook run. that's why we use the register keyword and debug module.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Thankyou !!

Hope you liked it!

If any query feel free dm me.














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

Suraj S.的更多文章

社区洞察

其他会员也浏览了