Roles
Definition: “Roles are a robust feature of Ansible that facilitate reuse and further promote modularization of configuration, but Ansible roles are often overlooked in lieu of straightforward playbooks for the task at hand. The good news is that Ansible roles are simple to get set up and allow for complexity when necessary” [1]
With roles we can define how things get built, what actions need to be taken after (i.e. restart web-server after config file is updated) and make all of this modular to be consumed by others.
Let’s use Ansible Galaxy to build our file structure for our role ansible_role
ansible-galaxy init ansible_role
Fig 8
Ansible Galaxy has now made our directory ansible_role
and the complete folder structure as shown below
Fig 9
Folder breakdown
- Tasks
main.yml - this is file will contain our plays; this serves as the same
tasks
section of our playbook
- Vars
main.yml - Variables you may wish to include in the role
- Handlers
main.yml - Here is where we’ll define actions to take. Example, your task changes a config file for Apache and you must now restart the Apache service. Your task would notify a handler to perform that action for you
1notify: restart-apache
2become: yes
1- name: restart-apache
2 systemd:
3 name: httpd
4 state: restarted
5 enabled: yes
6 become: yes
- Meta
main.yml - Add dependencies here for Ansible-Galaxy roles you want to include
Footnote