Deploying Web Server On Docker Using Ansible on EC2 instance

Apeksh Agarwal
4 min readAug 6, 2020

Do you want to configure multiple machines with same setup, but it takes a lot of time to do it manually and becomes even more difficult if the machines have different-different types of operating system ?

So Ansible tool helps to solve this issue. Ansible automates the complex tasks giving more time to developers to focus on other tasks. In other words, it frees up time and increases efficiency. Ansible is rapidly rising to the top in the world of automation tools. It is also known as heart and soul of DevOps industry.

What is Ansible ?

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.

To get more information, you can visit on their official site.

So in this blog as title shows, I will be deploying apache web server using Ansible on AWS EC2 instance. You can use your local VM also.

Necessary Requirements:

  1. Basic Knowledge of launching and managing instances in Cloud.
  2. Already installed Ansible.

So here are the steps:

Step 1: Launch your VM on which you want to setup your web server. Then keep a record of it’s IP address , username, password or key in case of cloud. That’s all you have to do in that VM and leave it running.

Note: Now all the steps are to be performed in Base OS where you have ansible tool.

Step 2: Create an Inventory file anywhere with any name. Inventory file contains all information of remote machines where you want to perform your operation. In my case i have created in etc directory.

vim /etc/myhosts.txt

Step 3: Enter the information you gathered in #Step 1.
In case of local VM you have to give
ansible_ssh_pass = <password> in case of ansible_ssh_private_key_file.
For more guide, you can visit here

Step 4: Create a ansible configuration file if not.

mkdir /etc/ansible
vim /etc/ansible/ansible.cfg

Now enter below path of Inventory file and disable host_key_checking in it.

Step 5: You can check your connectivity to manger node by below command

ansible all -m ping

If it is not pingable. Then you may have connectivity issues.

Step 6: Now you have to create a ansible playbook which contains all commands. You can create it anywhere with anyname but extension must be yaml or yml.
Then you have to enter commands for setting up web server.

Step 7: Now below will be steps for code completion. Below is the order you should follow while creating playbook to avoid any error.

  • Create yum repository for docker
  • Installing Docker from our repolist
  • Installing Python for using docker remote API
  • Installing docker-py python library using pip to run docker commands remotely
  • start docker service
  • copy html code from controller node to managed node
  • In the end creating docker container with httpd image, exposing port, mounting our volume

Below is my code for your reference:

You can also get this code from my github repo.

Step 8: Now run below command to run your playbook

ansible-playbook </path/to/your/playbbok/file>

It will look like this

After complete process, you can access your code from manged node IP along with exposed port and your file name.

If you are not able to connect, there can be issue of firewall security, you can stop by using below command

systemctl stop firewalld

So that was pretty much of it on how to deploy apache web server using Ansible on docker.

Thanks all of you for reading. See you in next blog.

--

--