Configuring Docker using Ansible

What is ansible ?

Ansible is a configuration management tool. It can be used for software provisioning and application deployment.

In this post I'll walk you through how you can install docker on target nodes and start apache web-server on top of it.

What is docker ?

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.

Stepwise Tutorial:

I'm using ubuntu os for our example.

  1. use this command to install latest version of ansible: sudo apt install ansible.
  2. By default we get two files at location /etc/ansible:
    1. ansible.cfg: contains all configuration settings of ansible
    2. hosts: contains list of target machines credentials we need to configure
  3. Edit ansible.cfg and uncomment inventory = /etc/ansible/hosts
  4. Edit hosts file and write ip of your target node in it.
$ vim /etc/ansible/hosts

# ip of your target node 
192.168.100.1
  1. Now we'll write ansible playbook to install docker and run apache webserver image on top of it.
- name: Configure webserver on docker container installed on target nodes 
  hosts: all 
  gather_facts: no 
  tasks: 
    - name: Install aptitude using apt
      apt: name=aptitude state=latest update_cache=yes force_apt_get=yes

    - name: Install required system packages
      apt: name={{ item }} state=latest update_cache=yes
      loop: [ 'apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common', 'python3-pip', 'virtualenv', 'python3-setuptools']

    - name: Add Docker GPG apt Key
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: Add Docker Repository
      apt_repository:
        repo: deb https://download.docker.com/linux/ubuntu bionic stable
        state: present

    - name: Update apt and install docker-ce
      apt: update_cache=yes name=docker-ce state=latest

    - name: Install Docker Module for Python
      pip:
        name: docker

    - name: Pull default Docker image
      docker_image:
        name: "httpd"
        source: pull
      become: yes 

    - name: Create default containers
      docker_container:
        name: "apache_server"
        image: "httpd"
        state: started
        ports:
          - "8080:80"
        tty: true 
        detach: true 

    - name: validate docker container 
      uri: 
        url: http://3.138.194.15:8080/
        return_content: yes 
      register: output
      failed_when: "'It works!' not in output.content"

Explanation:

  1. First we will install docker on remote machine which is given by first 6 tasks.
  2. Then we will pull official httpd image from docker hub
  3. In next step we will run docker container at port 8080
  4. In last step we check to see if our container was successfully launched or not.

How to run ansible playbook ?

→ We can run following command on our controller node to run ansible playbook.

$ ansible-playbook playbook_name.yml -u <username>

→ Playbook output will be as follows:

image.png

→ Checking for docker container on managed node.

image.png

As you can see from above screenshot, docker ps gives name of launched container as output

Now, if we visit managed node's url at port 8080, we will see our webpage.

→ Webpage served by managed node

image.png