jessefournier
jessefournier

Reputation: 193

How to include roles in another role

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

Upvotes: 3

Views: 4926

Answers (2)

jessefournier
jessefournier

Reputation: 193

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

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

Upvotes: 1

toydarian
toydarian

Reputation: 4554

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.

Upvotes: 8

Related Questions