Ansible: Write Ansible role to configure apache webserver
By: Shubham Rasal
Tags: Apache, DevOps, Ansible, Ansible Roles, Configuration Management

ANSIBLE-7

Ansible: Write Ansible role to configure apache webserver

What is ansible?

Ansible is an open-source IT Configuration Management, Deployment, and Orchestration tool. Ansible helps us to gain large productivity in a variety of automation challenges. This tool is very simple to use and also powerful enough to automate complex multi-tier IT application environments.

Ansible offers a simple architecture that doesn’t require special software to be installed on nodes. It also provides a robust set of features and built-in modules which facilitate writing automation scripts.

In this article, we are going to write an Ansible role to configure the Apache webserver. The Apache HTTP server is the most widely-used web server in the world. It provides many powerful features including dynamically loadable modules, robust media support, and extensive integration with other popular software.

You can also refer to this article, In this article, I have explained how to write a playbook for configuring the webserver.

In this guide, we are going to write an Ansible role to configure the Apache HTTP server.

but,

What is an Ansible role?

Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users.

An Ansible role has a defined directory structure with seven main standard directories. You must include at least one of these directories in each role. You can omit any directories the role does not use.

You can read more about the roles and directory structure in detail. Ansible roles.

Let’s start the action

Initialize ansible role. Read more about “ansible-galaxy”

#create directory and change directory $ mkdir roles

$ cd roles #create ansible role $ ansible-galaxy init myapache

This command will create a folder structure for a role.

I recommend you to read about the directory structure. Here the Ansible Role directory.

We want our role to configure apache for RedHat and Debian family. We can use this role to configure HTTPD for Redhat and apache2 for Debian Linux distribution.

What does this Role do?

Install httpd for RedHat or centos. Install apache2 for Debian. Create a custom document root folder for the new Apache VirtualHost and set up a test page. Enable the new Apache VirtualHost.

Let’s decide the variables that we will need.

we want to give access to the role user to override this variable so we will write these variables in the default/main.yml file. variables declared in this file can be overridden by role use at the time of using it.

Now we have declared the variable.

Apache Conf Template

The apache.conf.j2 the file is a Jinja 2 template file that configures a new Apache VirtualHost. The variables used within this template are defined in the /default/main.yml variable file.

#template/apache.conf.j2

Write tasks to configure httpd for the RedHat family.

The above tasks will configure httpd on RedHat distributions like RedHat Linux or Centos Linux.

Now let’s write tasks for configuring apache2 for Debian.

Now we have two task files. Now we need to use these files according to os type. We will include these files depending on the os type. So let’s include both the files (RedHat.yml and Debian.yml) in the main.yml file

We have also notified some handlers in the above tasks file. In the ansible role, we write handlers in handlers/main.yml file.

Now we have written all the necessary files. Let’s see the completed directory structure.

Now we can use this role, but before that, we have to tell ansible where you have stored your roles. You can add role location at ansible.cfg file.

You can directly add ansible roles path in /etc/ansible.cfg or you can also write ~/.ansible.cfg for user specific role.

now check your role is register and ready to use. using the below command.

$ ansible-galaxy role list

Let’s test this ansible role.

I have created two instances on AWS of ubuntu and RedHat Linux. Create an inventory file and add IP addresses and usernames to the inventory file.

Now add the ssh key.

$ ssh-add mykey.pem

Test that we can successfully connect to our managed nodes or not using the ping module.

$ ansible webserver -m ping -i inventory

Now write one playbook which will use our role.

Now we are ready to run and configure apache servers using an ansible role.

$ ansible-playbook mywebserver.yml -i inventory

Here’s a detailed output of the above playbook.

You can find the above playbook on this GitHub repository. Bookmark or star it which helps to keep me motivated.

If you have any doubts or something improvement needed in this blog, please feel free to reach out to me on my LinkedIn account.

I hope you learned something new and find ansible more interesting.

Let me know your thoughts about ansible and how do plan to use ansible?

This article is part of configuring load balancer and webserver using ansible role. The next part you can read here…

Thank you…

About the writer:

Shubham loves technology, challenges, is open to learning and reinventing himself. He loves to share his knowledge. He is passionate about constant improvements.

He writes blogs about Cloud Computing, Automation, DevOps, AWS, Infrastructure as code.

Visit his Medium home page to read more insights from him.

👋 Join FAUN today and receive similar stories each week in your inbox! ️ Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬