Hello everyone ! Today we are going to solve one of the use case or the task mentioned below :
By: Karthik Avula
Tags: Redhat Linux, Ansible, Arthbylw, Docker, Apache

Hello everyone ! Today we are going to solve one of the use case or the task mentioned below :

Write an Ansible PlayBook that does the following operations in the managed nodes:

🔹 Configure Docker

🔹 Start and enable Docker services

🔹 Pull the httpd server image from the Docker Hub

🔹 Run the docker container and expose it to the public

For this, you need to know about the tools like Ansible, Docker and some basic concepts of Linux Systems.

Ansible

Ansible is a Automation tool. What do I mean by automation ? It can be any one of Infrastructure Automation, Cloud Automation, Security Automation, Application Automation, many other !! Ansible simplifies it. With Ansible you can configure Routers, Clusters, Webservers, Applications, Docker and anything. As techies, we all know how to configure many services or clusters. What if we can do all the configurations mostly the intelligent configurations ( we don’t need to know how to do and where to do, we just need what to do ! ) in a single click ! Thats what Ansible is.

Ansible Communication Architecture includes one Controller Node, in which Ansible is installed and configured and a Target/Managed Node in which all the configurations can be done by Controller Node. Ansible has a inventory file which helps Ansible to know about the Managed Nodes by gathering some facts when a configuration is need to play/run. It has ad-hoc commands (which helps to run a particular play per a command) and a Playbook (in which all the ad-hoc commands are kept in yml format and runs when a playbook is started to run)

Docker

Docker is one of the famous containerization tool. With the help of Docker, we can launch our containers (which are like OS) within 3 seconds and do whatever operations we need. We can start our servers, run our code, programs etc

Now in this blog we will know how to launch our containers using Ansible and then we start to deploy our websites on Docker and expose it to public.

From the controller node, we need to configure Managed node and install docker there, for these we need to make a repository in which we keep our docker’s downloading URL. Since I have only one managed node, we can write all or managed node’s IP in hosts section.



tasks:

- name: Adding docker repo

yum_repository:

name: docker-ce

description: dockerREPO

baseurl:

gpgcheck: no

state: present - hosts: alltasks:- name: Adding docker repoyum_repository:name: docker-cedescription: dockerREPObaseurl: https://download.docker.com/linux/centos/7/x86_64/stable/ gpgcheck: nostate: present

After adding a Docker repo, we now need to install docker using command line. Since we are using “yum” command to install our docker in which we also passes nobest (such option is not present in ansible yum module) option we can directly uses the command module in Ansible. We are using docker-ce (Docker Community Edition).

- name: Installing docker using cmd

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

Now after installing Docker, we are now required to start our docker services.

- name: Starting Docker service

service:

name: docker

state: started

Now our docker service is started. For downloading images from Docker hub we need to use Ansible module called docker_image. In the documentation, it is writted that docker-py or docker file is needed to use this module in the best way. So I am using pip3 command and downloading docker-py file. You can use “pip3 install docker” too.

- name: Installing dependency file docker py

command: "pip3 install docker-py"

After downloading the dependency file, we will now pull our image from dockerhub to start our web server. Here I am pulling httpd image(official Apache HTTPD image).

- name: Pulling httpd image

docker_image:

name: httpd

tag: latest

source: pull

We will now start to create a directory in which we place our websites.

- name: creating webpages directory

file:

path: /doc_webpages

state: directory

After the creation of directory we will now copy our websites(index.html) to the same directory which we created.

- name: copying webpages

copy:

src: index.html

dest: /doc_webpages

After copying websites to the folder, we now start our container and here we are using port number 8080 to host our webpages and also we are now using the same folder in which we have our website as a volume and to docker we are sharing same folder as a volume to docker’s httpd container default document root.

- name: Launching container

docker_container:

name: WebServerhttpd

image: httpd

state: started

exposed_ports:

- "80"

ports:

- "8080:80"

volumes:

- "/doc_webpages:/usr/local/apache2/htdocs"

Now the entire playbook looks like



tasks:

- name: Adding docker repo

yum_repository:

name: docker-ce

description: dockerREPO

baseurl:

gpgcheck: no

state: present - hosts: alltasks:- name: Adding docker repoyum_repository:name: docker-cedescription: dockerREPObaseurl: https://download.docker.com/linux/centos/7/x86_64/stable/ gpgcheck: nostate: present - name: Installing docker using cmd

command: "yum install -y docker-ce --nobest" - name: Starting Docker service

service:

name: docker

state: started - name: Installing dependency file docker py

command: "pip3 install docker-py" - name: Pulling httpd image

docker_image:

name: httpd

tag: latest

source: pull - name: creating webpages directory

file:

path: /doc_webpages

state: directory - name: copying webpages

copy:

src: index.html

dest: /doc_webpages - name: Launching container

docker_container:

name: WebServerhttpd

image: httpd

state: started

exposed_ports:

- "80"

ports:

- "8080:80"

volumes:

- "/doc_webpages:/usr/local/apache2/htdocs"

I have given my playbook name as task10.yml . I can run my playbook using the command

ansible-playbook task10.yml

One way to check whether our playbook run successfully or not is to goto the url and whats there !

Yay ! we have successfully started docker service, started a container, copied websites to the container’s volume and exposed it to the public on port 8080.

Thank you all for reading this blog. Next time I will come with an another exciting blog.

This blog is a part of my journey in ARTH — The School of Technologies, guided by World Record Holder Mr. Vimal Daga sir !