Ansible playbook for setting up apache webserver in a docker container

Shivani Mandloi
3 min readDec 10, 2020

when you manually go for setting up apache server in docker container one has to first create a docker container and then go for further apache set up its ok till you want only one such container but there might be a requirement to get 100s and 1000s of docker container and not in single computer as per your situation come up you may need the same setup in other computers

so in such use case we can go for ansible, there are many options like shell scripting but again for that you will need the complete knowledge about each and every os which is pretty much time consuming where in ansible is the intelligent tool .

What is ansible and ansible playbook?

Ansible is simple a automation engine that automates cloud provisioning, configuration management , application development etc.

Ansible distribution have a controller node having ansible and number of manage nodes . Both controller node and manage node are connected through ssh. Using this distribution we complete same task parallel in every manage node, so that there is no waste of time.

Playbooks are the files where Ansible code is written. Playbooks are written in YAML format they are one of the core features of Ansible and tell Ansible what to execute. They are like a to-do list for Ansible that contains a list of tasks.

We need to create a file for details of manage node suppose ip.txt known as inventory.

Now to tell ansible that this is your inventory one has go in configuration file of ansible and do the following

#mkdir /etc/ansible

#vim /etc/ansible/ansible.cfg

now we have to create our code for play book so first lets create a proper algorithm for that

Step1: Configure yum

Step2: Configure Docker

Step3: Start and enable Docker service

Step4: pull the httpd server image from docker hub

Step5: Run the docker container and expose it to the public

Step 6: Copy the html code in /var/www/html directory and start the web server

Now, since anisble playbook supports YAML formate , we need to create a file with extension .yml and write code in it.

here first section is to configure yum repository because we dont know rather the target node has yum configured or not.

followed by module to install docker and the following.

now to rum this playbook we use command

#ansible-playbook <playbook name>

now we can cross check on our browser whether the service is started or not.

so here we can see that our webservice is successfully started

so using ansible one can do the same with many docker container within some minutes

hope you all find this interesting !!!!

thankyou!!!

--

--

No responses yet