Ansible Role is a concept that deals with ideas rather than events. Its basically another level of abstraction used to organize playbooks. They provide a skeleton for an independent and reusable collection of variables, tasks, templates, files, and modules which can be automatically loaded into the playbook. Playbooks are a collection of roles. Every role has specific functionality.
For example, to install Nginx, we need to add a package repository, install the package and set up configuration. Roles allow us to create very minimal playbooks that then look to a directory structure to determine the configuration steps they need to perform.
Role directory structure
In order for Ansible to correctly handle roles, we should build a directory structure so that Ansible can find and understand. We can do this by creating a Roles directory in our working directory.
The directory structure for Roles looks like this:
rolename
- files
- handlers
- meta
- templates
- tasks
- vars
A role's directory structure consists of files, handlers, meta, templates, tasks, and vars. These are the directories that will contain all of the code to implement our configuration. We may not use all of the directories, so in real practice, we may not need to create all of these directories.
Ansible will search for and read any yaml file called roles/nginx/tasks/main.yml automatically. Here is the main.yml file;
---
- name: Installs Nginx
apt: pkg=nginx state=installed update_cache=true
notify:
- Start Nginx
- name: Upload default index.php for host
copy: src=index.php dest=/usr/share/nginx/html/ mode=0644
register: php
ignore_errors: True
- name: Remove index.html for host
command: rm /usr/share/nginx/html/index.html
when: php|success
- name: Upload default index.html for host
copy: src=index.html dest=/usr/share/nginx/html/ mode=0644
when: php|failed
As we can see, the file just lists the steps that are to be performed, which makes it reads well.
We also made a change how we references external files in our configuration. Our src lines reference a static_files directory. This is unnecessary if we place all of our static files in the files subdirectory. Ansible will find them automatically.
Now that we have the task portion of the playbook in the tasks/main.yml file, we need to move the handlers section into a file located at handlers/main.yml.
- name: Start Nginx
service: name=nginx state=started
Move index.html and index.php pages out of the static_files directory and put them into the roles/nginx/files directory.
So now we can create a very very simple playbook with the following content:
---
- hosts: test_group
roles:
- role: nginx
Run it!
$ ansible-playbook -s test.yml
PLAY [test_group] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [127.0.0.1]
TASK: [nginx | Installs Nginx] ************************************************
ok: [127.0.0.1]
TASK: [nginx | Upload default index.php for host] *****************************
ok: [127.0.0.1]
TASK: [nginx | Remove index.html for host] ************************************
changed: [127.0.0.1]
TASK: [nginx | Upload default index.html for host] ****************************
skipping: [127.0.0.1]
PLAY RECAP ********************************************************************
127.0.0.1 : ok=4 changed=1 unreachable=0 failed=0