Skip to main content

Command Palette

Search for a command to run...

Ansible: Write Ansible role to configure apache webserver

Published
5 min read
Ansible: Write Ansible role to configure apache webserver

ANSIBLE-7

Write Ansible role to configure apache webserver on Redhat and Ubuntu.

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.

[What is Ansible? How ansible is helping companies in automation?
Factories that failed to automate fell behind due to so much competition in the market. That’s why automation became…developer-shubham-rasal.medium.com](https://developer-shubham-rasal.medium.com/what-is-ansible-acb573304acd "https://developer-shubham-rasal.medium.com/what-is-ansible-acb573304acd")

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.

[How to configure apache webserver using ansible?
Configure Apache server using ansible-playbook | Shubham Rasaldeveloper-shubham-rasal.medium.com](https://developer-shubham-rasal.medium.com/how-to-configure-apache-webserver-using-ansible-4b7077d88505 "https://developer-shubham-rasal.medium.com/how-to-configure-apache-webserver-using-ansible-4b7077d88505")

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?

  1. Install httpd for RedHat or centos. Install apache2 for Debian.
  2. Create a custom document root folder for the new Apache VirtualHost and set up a test page.
  3. 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.

[ShubhamRasal/ansible-playbooks
How to write an Ansible role to configure the apache server. github.com](https://github.com/ShubhamRasal/ansible-playbooks/tree/master/Roles/myapache "https://github.com/ShubhamRasal/ansible-playbooks/tree/master/Roles/myapache")

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…

[Create a role for setting up a load balancer and web server dynamically
Ansible role for setting up load balancer using HAProxy and webserver using Apache software.developer-shubham-rasal.medium.com](https://developer-shubham-rasal.medium.com/create-a-role-for-setting-up-a-load-balancer-and-web-server-dynamically-8f4e717eee30 "https://developer-shubham-rasal.medium.com/create-a-role-for-setting-up-a-load-balancer-and-web-server-dynamically-8f4e717eee30")

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 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

More from this blog

SREngineered - Shubham Rasal

26 posts

SREngineered Shubham Rasal - Inviting pragmatic engineers passionate about the craft. Delve into Kubernetes, AWS, Docker, Ansible, Golang, and more. Let's master engineering together!