Ansible roles: Configuring Web-server and HAproxy

Today we are going to configure httpd webserver and HAproxy using roles in ansible so, first what are Ansible roles?

Roles:

Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure

In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reusevand share them with other users.

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

For more information click here: ansible roles

creating Roles:

For creating roles, We use the following command:

ansible-galaxy role init <role_name>

Now role for webserver:

Screenshot 2021-03-28 174434.jpeg

We can see the directory structure of the role:

Screenshot 2021-03-28 174522.jpeg

Now change directory to the files , this directory contains the files which we want to use or transfer:

Screenshot 2021-03-28 181306.jpeg

index.php:

Screenshot 2021-03-28 174925.jpeg

Now to change directory to vars and create main.yml where you can write all the variables:

7euSJickz.jpeg

# vars file for webserver
  packages:
        - httpd
        - php

Now change directory to tasks , this file contains the main file but without task:

p3GkuaWeb.jpeg

main.yml:

# tasks file for webserver
- name: To install Packages
  package:
          name: '{{ item }}'
          state: present
  loop: '{{ package_names }}'

- name: To start webserver
  service:
          name: httpd
          state: started

- name: Copy
  copy:
          src: 'index.php'
          dest: '/var/www/html/index.php'

We have finally configured Web server.

Now to create a role for Configuring Load-Balancer:

Screenshot 2021-03-28 175335.jpeg

For configuration of Load-Balancer first of all, we have take the normal haproxy.cfg file and use jinja templating to edit it so that it automatically add IP's of webservers to the file and put this file in templates directory.

zFLBKW8Xu.jpeg

haproxy.cfg:

9GkXA_BD8.jpeg

using jinja templating:


backend app

    balance     roundrobin
{% for  ip  in  groups['webservers'] %}
    server  App{{ loop.index }}  {{ip}}:80  check
{% endfor %}

Change directory to tasks for main.yml :

rrijvzVpw.jpeg

---
# tasks file for loadbalancer
- name: To install Haproxy
  package:
          name: haproxy
          state: present

- name: To start Haproxy
          name: haproxy
          state: started

- name: To upload configuration file for Haproxy
  template:
          src: "haproxy.cfg.j2"
          dest: "/etc/haproxy/haproxy.cfg"

So our Load-balancer role is successfully configured, now it's time to combine both the roles:

mainrole.yml

KFMCxYRS_.jpeg

Now we run the playbook using:

ansible-playbook mainrole.yml

Through this playbook our webserver and loadblancer are successfully configured, after searching the IP address of the load-balancer machine we can check the Webserver and Load Balancer working properly therefore our task is successful.