Configuring HTTP server on Docker by Ansible

Here in this article, we gonna see how to setup docker configuring with the help of ansible. We will launch an os of HTTP server and configure completely on the top of docker by ansible. So, let’s start

Pre-requisite(on localhost)

  1. sshd enabled
  2. Boto3 library
  3. and ec2.ini (for getting info about running instances on aws)

Steps to make ansible playbook

  1. create repo for docker
  2. install docker-ce
  3. start docker service
  4. install python3
  5. install docker sdk by pip
  6. pull image
  7. create a directory for persistent storage
  8. dump the web files in the directory
  9. launch the os by attaching port and volume

Now lets’ create the playbook

Step1. setting the host name

On the top AWS I had launched an os with tag name: docker and the host is added to group [tag_name_docker] which we will use to provide hosts to the playbook

- hosts: tag_name_datanode

Step2. creating docker repo

For this we will use yum_repository module which helps us to create repo with our desired baseurl .

- name: creating docker yum
name: docker
baseurl: ""
description: "Docker repo"
gpgcheck: no

Step3: installing docker-ce

For installing docker here we use command module bcz no module is available in ansible in which we pass — nobest.

- name: installing docker
cmd: sudo yum install docker-ce --nobest -y

Step4: Starting docker services

Here we use service module to start the docker services

- name: starting docker services
name: docker
state: started

Step5: Install python3

Here we are installing python3 for installing the docker sdk which helps to contact with docker hub. For this we use package module

- name: installing python
name: python36
state: present

Step6: Installing docker sdk

By the use of pip3 command we can install docker sdk

- name: installing docker sdk
name: docker

Step7: pulling image

By the use of docker_image module we will pull docker images from the docker hub.

- name: pulling image
name: "{{image_name}}"
source: pull

Step8: creating a directory

Here we create a new directory in the base os for storing the data of docker os otherwise as soon the os goes down the whole data will be loosed. For this we use file module.

- name: creating direc
path: "{{direc_path}}{{os_name}}{{item}}"
state: directory
with_sequence: count={{ no_of_os }}

here {{ *** }} is used for variables and the values of variables will be stored in another file . This task contains the for type loop, so that a specific type of sequence will differentiate between more than one directory.

Step9. Dumping files in the directory

Now we will transfer the web pages to created directories.

- name: dumping files
src: "{{file_name}}"
dest: "{{direc_path}}{{os_name}}{{item}}"
with_sequence: count={{no_of_os}}

Step10. Launching the os

Now start the os with the image_name, port_no and with os_name

- name: launching an os in docker
name: "{{os_name }}{{item}}"
recreate: yes
image: "{{ image_name }}"
- "{{ exposed_port|int + item|int }}:{{os_port}}"
- "{{direc_path}}{{os_name}}{{item}}:{{path_to_mount}}"
with_sequence: count={{ no_of_os }}

Let’s build the whole enviroment with a single command

ansible-playbook docker.yml

here is the git_hub link for the roles

Thank You!

Devops Enthusiast