Configuring Web-server and HAproxy using Ansible roles

Configuring Web-server and HAproxy using Ansible roles

In today's task, we again going to configure Web-server and Load-balancer using Ansible, but this time we are gonna use Roles .

What are Roles?

Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules.

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 reuse.

Each role is basically limited to a particular functionality or desired output, with all the necessary steps to provide that result either within that role itself or in other roles listed as dependencies.

Roles are not playbooks. Roles are small functionality which can be independently used but have to be used within playbooks. There is no way to directly execute a role. Roles have no explicit setting for which host the role will apply to.

By default, Ansible will look in each directory within a role for a main.yml file for relevant content (also main.yml 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.

My Inventory:-

[webservers]
192.168.43.211 ansible_user=root ansible_password=98779877 ansible_connection=ssh

[load_balancer]
192.168.43.219  ansible_user=root ansible_password=98779877 ansible_connection=ssh

My Ansible Configuration File:-

[defaults]
inventory=/root/Extra/ip.txt
host_key_checking = false
ask_pass = false

How to create Roles?

For creating roles, We run the following command:-

ansible-galaxy role init <role_name>

This will create a Role in the same folder but if you want to store your Roles in a different location than /etc/ansible/roles/ , set the roles_path configuration option so Ansible can find your roles.

webserver role created.jpg

If you want to see the directory structure of the role:-

tree <role_name>

tree.jpg

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

main created.jpg

main.yml:-

# vars file for webserver
  packages:
        - httpd
        - php

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

index.jpg

index.php:-

<pre>
<?php
print `/usr/sbin/ifconfig`
?>
</pre>

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

task main yml.jpg

main.yml

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

- name: Starting webserver
  service:
          name: httpd
          state: started

- name: Copying
  copy:
          src: 'home.php'
          dest: '/var/www/html/home.php'

So we have finally configured Web server .

Now we create a role for Configuring Load-Balancer:-

loadbalancer role.jpg

The configuration of Load-Balancer is almost same as the Web-Server.

First of all, change directory to templates and take the normal haproxy.cfg file and by using jinja templating edit it so that it automatically add IP's of webservers to the file:-

templates.jpg

haproxy.cfg:-

haproxycfg.jpg

#------------------------------------------------------------------
backend app

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

Change directory to tasks and create main.yml :-

task main yml lb.jpg

main.yml

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

- name: Starting Haproxy
          name: haproxy
          state: started

- name: Uploading 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:-

To combine both roles we have to create a yml file which can run both roles.

mainrole.yml

- hosts: load_balancer
  roles:
          - role: loadbalancer

- hosts: webservers
  roles:
          - role: webserver

ansible main.jpg

Now we run the Playbook by using:-

ansible-playbook mainrole.yml

webserver configured.jpg load balancer configured.jpg

Now search the IP address of the load-balancer machine in Browser to check if the Webserver and Load Balancer working properly or not.

webbrowser.jpg

Now as you can see the Webserver and Load Balancer working properly means our task is successful.