Ansible 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.
Command to create a role is
ansible-galaxy role init <role_name>
Configure a webserver
Now as in this we need a role to configure web server so it is logical to give role_name as webserver.
Now change your directory to webserver and write then to vars to enter the variables.
# vars file for webserver
packages:
— httpd
— php
Now change your directory to files and write a php code to show the ip.
<pre>
<?php
print `/usr/sbin/ifconfig`
?>
</pre>
Now change directory to tasks , this file contains the main file but without tasks.
create a main file here using vim main.yml
- 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’
Now configure Loadbalancer
Create an ansible role as mentioned above with name loadbalacer
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.
Change directory to tasks and create main.yml file
# 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”
Now combine them both .
Create a new file namely mainrole.yml in the same location of roles.
- hosts: load_balancer
roles:
— role: loadbalancer- hosts: webservers
roles:
— role: webserver
Now play this playbook and check your ip in browser.
Hence the task is successfully done!