Deploying a webserver container through Ansible-playbook

B.V.Rohan Bharadwaj
3 min readAug 2, 2020

--

What is Ansible? Why use it?

It’s an open source software(CLI) meant for provisioning and configuration management . Ansible can also double as a application deployment tool via IaC.

As it’s free and flexible we can use it for multiple situations , ranging from simple configurations like yum , docker to provisioning a cloud application and so on.

What’s a “ playbook ” ?

It’s a simple file written in YAML ( which means it’s entered in a key-value pair format) . Ansible has its own few keywords to follow but it’s pretty simple and readable

Task Objectives:

🔹 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 web server

Procedure :

1] Installing required software and tools:

we start off with :

pip3 install ansible

then:

yum install sshpass

Next thing to do is , to configure our inventory.

In the location: /etc/ansible we can find a file called “ hosts

vim /etc/ansible/hosts

This will give you access to the file and enter:

<your_ip> ansible_host_key_checking=false  ansible_ssh_user=root ansible_password=<your_password>

Now You can specify a different inventory file at the command line using the -i <path> option (if needed).

2] YAML file:

i] Configuring the yum repository for docker :

- name: Configuring Webserver using Docker
hosts: <your_IP_here>
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

ii] Installing docker in that node:

- name: Installing The Docker
hosts: <your_IP_here>
tasks:
- name: Installing the Docker
yum:
name: docker-ce-18.09.1-3.el7.x86_64
state: present

iii] Pulling the image from dockerhub :

- name: Pulling a image
hosts: <your_IP_here>
tasks:
- name: pull
docker_image:
name: httpd
source: pull

iv] Making a Directory :

- name: Creating a folder in Node
hosts: <your_IP_here>
tasks:
- name: creating the folder
file:
path: <location_of_choice>
state: directory

v] Now all that’s left is , to start the docker services and with that the ip is exposed with port 80 with 9999

- name: Create a data Container
hosts: <your_IP_here>
tasks:
- name: Create a Data Container
docker_container:
name: Webserver
image: httpd
state: started
exposed_ports:
- "80"
ports:
- "9999:80"
volumes:
- <location_of_the_dir>

vi] Running the playbook:

using the command :

ansible-playbook <yaml_file_name>.yml 

Next we have to check at the node’s end if everything is up and running ( for confidence of course! )

vii] Testing:

For that , you need to enter the IP of your remote connected( /managed ) node in a browser with the port number 9999 and /<file_name>.html at the end.

To keep it simple, I entered that.

And that’s how easy it is to deploy a webserver container through Ansible-playbook

You may find the complete doc in my github profile:

Thank you for the time!

--

--