Ansible Role

To reuse role in a playbook, there are 3 options:

  • roles: at play level, statically, executed before tasks.
  • include_role: at task level, dynamically, parsed and executed at where it is defined.
  • import_role: at task level, statically, parsed before the calling play execution and if no syntax error executed at where it is defined.

Regarding statically and dynamically above, there is explanation.

Example snippet for role reusing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
---
- hosts: all
gather_facts: no
any_errors_fatal: true
become: true
roles:
# The same as import_role module.
- <role1 name>
- <role2 name>

tasks:
# Dynamiaclly load, parse and run role in playbook where it is defined.
- name: Include role
include_role:
name: <role name>

# Statically load role, the same as 'roles' clause that will parse the
# imported role before playbook runs, it will fail the playbook if there is
# any syntax error.
- name: Import role
import_role:
name: <role name>
0%