Ansible Roles

Create the simple website which is running on apache web servers using Ansible roles


In this article we are going to cover -

  • Creating the new role called linuxtechiebulb which includes the Installation of the Apache web server.
  • Create a new DocumentRoot folder.
  • Setting up the file permissions.
  • Create a new Apache vhost template file.
  • Restart the Apache to get the website up.


  • Two CentOS/any Linux OS installed machine, one for Ansible installation and other for remote node.
  • Proper networking access between the machines and outside world.
  • I assume that you have the basic working knowledge of Ansible.

In this article we are using the two machines.

  • CentOS Linux release 7.2.1511 ( where Ansible server must be installed.
  • CentOS Linux release 7.2.1511 ( where we are going to run the playbook.

Brief Overview of Ansible

Ansible is an IT automation software developed by Michael DeHaan and owned by Red Hat. Ansible facilitates the automation of common IT tasks such as configuration management, deploying and updating applications on premises or in the cloud and creating development environments.

Ansible have three major components -

  • An automation language that describes application infrastructure in Ansible Playbooks.
  • An automation engine that runs Playbooks.
  • An enterprise framework, called Ansible Tower, that controls, secures and manages automation.

Ansible Installation

For Ansible installation and configuration, please check my previous article.

Ansible Installation & Configuration


Our main aim is to build the simple website using Ansible role which we will be achieve to perform the below tasks -

  • Install Apache web servers
  • Create a new DocumentRoot folder
  • Copy index page (index.html) to the Document root
  • Setup permissions for DocumentRoot folder and files
  • Create a new Apache vhost template file
  • Restart Apache to apply the changes
  • Test the webpage is up and running

Ansible Roles

The Ansible roles is the organized way to perform the tasks in different playbooks according their functionality in a directory structure way. In role inside every directory we have the main.yml file. Most common role directory structure is below -

  • tasks (a series of tasks are defined here)
  • handlers (all handlers are defined here)
  • vars (all the variables are defined here)
  • meta (all the meta-data defined here (It is used to declare role dependences))
  • files (all the files put inside this directory)
  • templates (Jinja2 like format template files are defined here)

We are using the role name is “linuxtechiebulb”. First, we will create a “linuxtechiebulb” role with its subfolders like the below -

# cd /home/somesh/Ansible/

# ls -R linuxtechiebulb/



After that, we will create a “linuxtechiebulb” role with its subfolders and main.yml files like the below.


# ls -R linuxtechiebulb/



1. First we insert/create the index file code in the “index.html” file in “files” folder as mentioned below.

# vim /home/somesh/Ansible/linuxtechiebulb/files/index.html




2. After that insert/create the metadata code in the “main.yml” file in “meta” folder as mentioned below.We are creating the very simple website, we don’t have any dependency so, we put the metadata file empty as mentioned below.

3. After that insert/create the handlers code in the “main.yml” file in “handlers” folder as mentioned below.

4. After that insert/create the variables code in the “main.yml” file in “vars” folder as mentioned below.

5. After that insert/create the Jinja template code in the “vhost.j2” file in “templates” folder as mentioned below.

6. After that insert/create the tasks code in the “main.yml” file in “tasks” folder as mentioned below.

Note: In this example – We are automating the copying of the file “index.html” from the Ansible server ( to remote server (, remote location as mentioned in subsection 4 above.

Our “linuxtechiebulb” role is completed here. Now move to next step to create the main tasks file to put all the elements together.

Running Ansible Roles

Before running our new “linuxtechiebulb” role, we need to set the roles_path in the /etc/ansible/ansible.cfg file. In our case the roles_path is:

Till now we have just created the role but not called. So in our case, we want to run the playbook on another machine/hosts “”. Now we will call the role using playbook as mentioned below.

# vim /home/somesh/Ansible/linuxtechiebulb/linuxtechiebulb.yml


Now all set and it’s time to run our playbook now. We will run our playbook as mentioned below.

In above snapshot we can see the step by step running status of playbook with each task and total changes is 8. Now move on next step to check the site is up and running on remote machine/hosts “”.

Final Step

We seen in last step the playbook run successfully. Now it’s time to verify on remote machine (

Here the website is up and running. It’s all done .. 🙂 🙂



Leave a Reply

Your email address will not be published. Required fields are marked *