Creating Ansible Roles for haproxy and httpd

Gaurav Tank
5 min readApr 8, 2021

Ansible is a handy configuration management tool. It helps in configuring a large number of servers from one single controller instance. It helps automate complex tasks like configuration management, application deployment, creating CI/CD pipelines, etc. Writing ansible code to manage the same service for multiple environments or different products increases code redundancy. With more complexity in functionality, it becomes difficult to manage everything in one ansible-playbook file. Sharing code among teams becomes difficult. So to solve this problem we need to create ansible roles that are easier to share.

What is an Ansible role?

Ansible role is an independent component that allows the reuse of common configuration steps. It is a set of tasks to configure a host to serve a certain purpose like configuring a service. Roles are defined using YAML files with a predefined directory structure.

So now we are going to see how to create ansible roles for apache and haproxy server and then we are going to create this setup over AWS instances.

STEP1: Creating the role for Apache webserver.

We can create a new role by ansible-galaxy init httpd

Here, I have initialized a new role with the name, httpd. It will create a new dir containing various other dir.

Contents of a Role

Now, we need to write all the tasks in the tasks dir, copy all the necessary templates in templates dir.

So, in tasks/main.yml we will write all our tasks.

---
# tasks file for httpd
- name: "installing the packages"
package:
name: "{{item}}"
state: present
loop:
- httpd
- php
- name: "Copying the webpage"
template:
src: index.php.j2
dest: "/var/www/html/index.php"
- name: "Copying essential"
template:
src: index.html.j2
dest: "/var/www/html/index.html"
- name: "Starting the service"
service:
name: httpd
state: started

This is the example of the task where we installed all the essential packages- httpd: for the services, PHP: for the webpage. Then we started the httpd services and copied the web page from templates dir to the home dir of apache webserver that is, /var/www/html. At last, we started the services.

index.html
index.php

Now our first role is created.

STEP 2: Creating a role for haproxy server

We can initialize it by the command ansible-galaxy init haproxy

It will also create the dir same as above with all the content. Here, we need to write all the tasks in tasks/main.yml file

---
# tasks file for haproxy
- name: "Installing package"
package:
name: haproxy
state: present
- name: "Configuring the services"
template:
src: haproxy.cfg.j2
dest: "/etc/haproxy/haproxy.cfg"
- name: "Starting the service"
service:
name: haproxy
state: started

Here we installed the haproxy package then copied the customized configuration file and then started the services.

In this role, the most important part is the haproxy.cfg.j2 file. We need to customize it according to our needs. So I changed the binding port to 1234 from 8080 and wrote a jinja 2 code for its dynamic configuration.

haproxy.cfg

This code will automatically pick all the IPs that come under the web group in the ansible inventory and write them there automating this part.

With this, our haproxy role is also finalized.

STEP 3: Launching ec2 instances over AWS

Our final goal was to launch this setup over the AWS instance. So we can create instances either by ansible-playbook or by ansible roles. Here to make it simple I am using a playbook.

- hosts: localhost
vars_files: "/etc/ansible/aws_key.yml"
tasks:
- name: "Creating a new ec2"
ec2:
key_name: "[[ Key }}"
instance_type: "t2.micro"
image: "ami-0742b4e673072066f"
wait: yes
count: 1
instance_tags:
Name: aws_ansible_ec2
vpc_subnet_id: "{{ subent }}"
assign_public_ip: yes
region: "us-east-1"
state: present
group_id: "[[ sg }}"
aws_access_key: "{{ Access_key }}"
aws_secret_key: "{{ Secret_key }}"
register: ec2
- debug:
var: ec2.instances[0].public_ip
- blockinfile:
path: "/etc/ansible/NEW/ip"
block: |
[web]
"{{ ec2.instances[0].public_ip }}"

This playbook will automatically launch an ec2 instance and update the inventory for further configuration. Similarly, we can launch one instance for the haproxy server, and some more instances to configure the Apache webserver.

Here, I’ve launched one instance LB for haproxy and httpd1 and httpd2 as the webserver.

STEP 4: Final step

To run all the roles and the scripts we need one master script. By running that master script all the configuration will be done automatically.

Create a new file for the playbook vim play.yml

- hosts: web
roles:
- httpd
- hosts: ec2
roles:
- haproxy
~

After launching the instance on AWS we can run play.yml to automatically run the roles.

RESULT:

Here, we can observe that with only one IP i.e. of the haproxy server we can balance the load of the two webservers.

GitHub URL for your reference.

Hope you find this blog informatinve

Thank you for reading!!

--

--