Using Roles in Playbooks - Ansible Tutorial
Introduction to Roles
In Ansible, roles are a way to organize playbooks and tasks. Roles allow you to break down a playbook into reusable components, making it easier to manage and maintain. Each role is a collection of tasks, variables, files, templates, and modules that can be used to perform a specific function.
Creating a Role
To create a role, you can use the ansible-galaxy command. This command will generate the necessary directory structure for your role.
Command:
ansible-galaxy init my_role
This will create a directory named my_role
with the following structure:
my_role/ ├── defaults │ └── main.yml ├── files ├── handlers │ └── main.yml ├── meta │ └── main.yml ├── tasks │ └── main.yml ├── templates ├── tests │ ├── inventory │ └── test.yml └── vars └── main.yml
Using Roles in Playbooks
Once you have created a role, you can use it in your playbooks. To include a role in a playbook, use the roles keyword.
Example Playbook:
--- - hosts: webservers roles: - my_role
In this example, the my_role
role will be applied to all hosts in the webservers
group.
Defining Tasks in a Role
Tasks are defined in the tasks/main.yml
file within the role directory. Here, you can specify the tasks that should be executed as part of the role.
Example tasks/main.yml
:
--- - name: Ensure Apache is installed apt: name: apache2 state: present - name: Start Apache service service: name: apache2 state: started
In this example, the role will ensure that Apache is installed and started on the target hosts.
Using Variables in Roles
Variables can be defined in the vars/main.yml
or defaults/main.yml
file within the role directory. Variables in defaults/main.yml
have the lowest precedence, while variables in vars/main.yml
have a higher precedence.
Example defaults/main.yml
:
--- apache_port: 80
Example tasks/main.yml
using variables:
--- - name: Ensure Apache is installed apt: name: apache2 state: present - name: Start Apache service service: name: apache2 state: started - name: Ensure Apache is listening on the correct port lineinfile: path: /etc/apache2/ports.conf regexp: '^Listen' line: "Listen {{ apache_port }}"
Using Handlers in Roles
Handlers are used to perform actions when a task reports a change. Handlers are defined in the handlers/main.yml
file within the role directory.
Example handlers/main.yml
:
--- - name: Restart Apache service: name: apache2 state: restarted
You can notify handlers from tasks using the notify keyword.
Example tasks/main.yml
with notifications:
--- - name: Ensure Apache is installed apt: name: apache2 state: present notify: - Restart Apache
Templates in Roles
Templates are used to generate configuration files from Jinja2 templates. Templates are stored in the templates
directory within the role directory.
Example template apache2.conf.j2
:
Listen {{ apache_port }}DocumentRoot /var/www/html
You can use the template module in your tasks to deploy these templates.
Example tasks/main.yml
using a template:
--- - name: Deploy Apache configuration template: src: apache2.conf.j2 dest: /etc/apache2/apache2.conf notify: - Restart Apache
Conclusion
In this tutorial, we've covered the basics of using roles in Ansible playbooks. Roles help you organize your playbooks into reusable components, making them easier to manage and maintain. By using roles, you can define tasks, variables, handlers, and templates in a structured way, leading to more modular and maintainable Ansible code.