Ansible Roles : Configuring HTTPD Web Server & HAProxy LB

Tilwani Hardik
5 min readJun 17, 2022
Source : Owner
  • Hello there, in this article we will learn about “ Ansible Roles ” & their important role managing “ Playbooks ”.
  • Here I will be creating Two roles, one role for configuring webservers and another for configuring the load balancer (reverse proxy).

What are Ansible Roles ???

  • Roles let you automatically load related vars, files, tasks, handlers, and other Ansible artifacts based on a known file structure. After 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 eight 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.

Example Structure of Role Directory :

# playbooks
site.yml
webservers.yml
fooservers.yml
roles/
common/
tasks/
handlers/
library/
files/
templates/
vars/
defaults/
meta/
webservers/
tasks/
defaults/
meta/

By default Ansible will look in each directory within a role for a main.yml file for relevant content (also main.yaml and main):

  • tasks/main.yml - the main list of tasks that the role executes.
  • handlers/main.yml - handlers, which may be used within or outside this role.
  • library/my_module.py - modules, which may be used within this role (see Embedding modules and plugins in roles for more information).
  • defaults/main.yml - default variables for the role (see Using Variables for more information). These variables have the lowest priority of any variables available, and can be easily overridden by any other variable, including inventory variables.
  • vars/main.yml - other variables for the role (see Using Variables for more information).
  • files/main.yml - files that the role deploys.
  • templates/main.yml - templates that the role deploys.
  • meta/main.yml - metadata for the role, including role dependencies.

Documentation for reference :

Creating role for webservers :

  • If you store your roles in a different location than /etc/ansible/roles/, set the roles_path configuration option so Ansible can find your roles.
  • Here you have to mention the roles_path in the ansible config file.
  • For creating roles, We need to run the following command —
ansible-galaxy role init <role_name>
  • Here I have created the role named “ MyWebservers ”.

NOTE : Here I am doing this demo by launching instances in AWS cloud, you can do it in a local system by launching multiple VM(s)

  • The files inside the role named MyWebservers are as shown below —
  • Now change to the directory /tasks and write the following code in “main.yml”
  • The “files/” directory contains the files or configuration files which we need to use. Change to this directory and keep your webpage there.
  • Here I have a file named “index.php” that is my webpage.
  • Here, We are done with the configuration of the webserver.
  • This will —
Install httpd software
Start the httpd service
Copy the webpage

Create role for Configuring the load balancer —

  • Here I have created the role named “MyLoadBalancer” which consists :
  • Change the directory to “tasks/”.
  • This directory contains all the tasks which we are gonna use. Edit the main.yml file and write the following code :
  • For configuring the load balancer this will execute the following :

Install the haproxy software
configure the haproxy.cfg(configuration) file
start the haproxy service

  • In haproxy.cfg file write the following :
  • Open the haproxy.cfg file in the controller node and bind the port 8080. Also, write the below-mentioned jinja code to update the haproxy.cfg file to load balancer dynamically.
  • Now we are done with the configuration of LoadBalancer.

Combining the roles together —

  • For using the roles we need to make a playbook that will run those roles. Here I have created the main_setup.yml file which is the main playbook we are going to run.
  • Now our final setup looks like this :

The code in main_setup.yml is as follows :

  • Now let us execute this playbook using :

ansible-playbook main_setup.yml

  • Finally, The playbook ran successfully without any error, That means the Webservers and Load Balancer are configured successfully.
  • Let us Verify, For this, We will access our webservers using the IP on which Load Balancer is configured and the port number 8080 which we bound in the “ haproxy.cfg ” file.
  • Here you can see we are connected to 172.31.39.146, which Is one of our Webserver IP , when I will refresh then it will connect to another Webserver having IP 172.31.33.19

Github repository link for reference :

Thank You so much for reading & learning with me.

--

--