Pages

Monday, February 22, 2016

Ansible Roles

Share it Please
Unilt now we have seen how we can write playbooks and using them with Ansible-playbook command to run them on the remote machine. Ansible playbook is very usefull when we have a limited number of machines and a limited number of changes that needs to be done the remote machine.

Consider a case where we want to configure a system with multiple packages, edit the configuration files using different variables, add files to be copied to remote machines , and add multiple handlers. For this case it is very hard to add all this in one playbook.

Ansible roles are a further level of abstraction that can be usefull for organizing playbook.s
As you add more and more functionality and flexibility to your playbooks, they can become unwieldy and difficult to maintain as a single file. Roles allow you to create very minimal playbooks that then look to a directory structure to determine the actual configuration steps they need to perform.

Organizing things into roles also allows you to reuse common configuration steps between different types of servers. This is already possible by "including" other files within a playbook, but with roles, these types of links between files are automatic based on a specific directory hierarchy.

In this article we will see how we can write a simple role in for installing Apache on the remote machine. Create a directory structure as below.

[root@vx111a roles]# tree apache/
apache/
── files
── handlers
── meta
── tasks
── templates
└── vars

The directories are explained below

Directory
Description
tasks
The tasks folder should contain a main.yml file, which should include a list of the tasks for this role. Any task includes that are contained in these roles will look for their files in this folder also. This allows you to split a large number of tasks into separate files, and use other features of task includes.
files
The files folder is the default location for files in the roles that are used by the copy or the script module.
templates
The templates directory is the location where the template module will automatically look for the jinja2 templates included in the roles.
handlers
The handler’s folder should contain a main.yml file, which specifies the handlers for the roles, and any includes in that folder will also look for the files in the same location.
vars
The vars folder should contain a main.yml file, which contains the variables for this role.
meta
The meta folder should contain a main.yml file. This file can contain settings for the role, and a list of its dependencies. This feature is available only in Ansible 1.3 and above.

Now lets create a necessary files for running the playbook. For the article purpose we will keep this simple.

1) Create a main.yml file in tasks directory which contains the necessary instructions for executing this.

[root@vx111a roles]# cat apache/tasks/main.yml
---
   - name: Install Apache
     yum: pkg=httpd state=latest
     notify:
     - restart apache

2) Now since we have defined the notify we need to define the handler which is created in the handlers folder as

[root@vx111a roles]# cat apache/handlers/main.yml
---
 - name: restart apache
   service: name=httpd state=restarted

Now when you come out the apache folder and use the tree command we can see the directory structure as,

[root@vx111a roles]# tree apache/
apache/
── files
── handlers
│   └── main.yml
── meta
── tasks
│   └── main.yml
── templates
└── vars

We have the main.yml file in both tasks and handler only as we have not defined any variables (var folder) , files to be copied to remote location ( file folder ) and any templates to be editited ( templates folder).
Now let’s create our main playbook file to running the apache role on the remote machine as,
[root@vx111a roles]# cat play.yml
---
 - hosts: cent
   roles:
     - apache

We just defined the role name in the playbook using the roles element.This will make the Ansible playbook command to check for the apache role (directory structure) for running the apache role playbook on the remote machine. The play.yml is located beside the directory apache (not inside the directory). Once we execute the playbook we can see,
[root@vx111a roles]# ansible-playbook play.yml
PLAY [cent] ******************************************************************
GATHERING FACTS ***************************************************************
ok: [172.16.202.96]

TASK: [apache | Install Apache] ***********************************************
changed: [172.16.202.96]

NOTIFIED: [apache | restart apache] *******************************************
changed: [172.16.202.96]

PLAY RECAP ********************************************************************
172.16.202.96              : ok=3    changed=2    unreachable=0    failed=0  


Thus the basics of Roles in Anisble. In the next article we will see a more advanced example of using  roles in Ansible.

1 comment :