2

I have organized all my playbooks into roles. Now that I have all the roles, I'm trying to create a new role that would include all the other roles.

In Ansible, is it possible to create a role that just calls other roles? If so, is it possible to do it as a list, like in a playbook:

---
- hosts: webservers
  roles:
  - role1
  - role2
  - role3
toydarian
  • 4,246
  • 5
  • 23
  • 35
jessefournier
  • 181
  • 1
  • 2
  • 13

2 Answers2

5

You can, for example, add them as dependencies in the meta/main.yml:

dependencies:
  - role: role1
  - role: role2
  - role: role3

Take a look at the documentation.

Alternatively you can use import_role or include_role (example below for the include version).

- include_role:
    name: 'role1'

# or with a loop
- include_role:
    name: "{{ item }}"
  loop:
    - role1
    - role2
    - role3

The different options to use a role are all synthesized in the role documentation - Using roles

But if I were you, I would not create a role that contains all other roles, but include them in a playbook. Adding them in a role doesn't give you any value.

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
toydarian
  • 4,246
  • 5
  • 23
  • 35
  • Using dependencies makes installing the required roles much easier, as `ansible-galaxy` will pull them automatically for you when installing the parent role. – Iizuki May 23 '23 at 08:00
1

I solved my issue by adding the following in meta/main.yml:

---
dependencies:
  - role: role1
  - role: role2
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
jessefournier
  • 181
  • 1
  • 2
  • 13
  • 1
    I strongly suggest you read the last warning in @toydarian answer and limit role dependencies to the very strict minimum necessary. Practice will most probably show you that it is very hard to manage and maintain. – Zeitounator Aug 04 '21 at 10:23