ansible: include role in a role?

Michael picture Michael · Oct 24, 2014 · Viewed 48.3k times · Source

Is it possible to reuse a role in a role? I do not mean via defining a dependency in the meta/main.yml file of a role but by including the role in the tasks/main.yml of another role directly?

For example, I define a couple of basic roles in rolebooks and some more high level roles in roles. I want the high level roles to include some of the basic roles in addition to some specific tasks.

playbooks/

  rolebooks/
    some_role/

  roles/
    webtier/
      tasks/
        main.yml

In playbooks/roles/webtier/tasks/main.yml:

- shell: echo 'hello'
- { role: rolebooks/some_role }
- shell: echo 'still busy'

Thanks

Answer

masu picture masu · Oct 8, 2016

Old question BUT for the record: use Ansible 2.2+ and you're good to go with include_role. Exactly for this very purpose... see documentation here.

Check out import_role as well... see documentation here