Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible: include role in a role?

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

like image 435
Michael Avatar asked Oct 24 '14 15:10

Michael


People also ask

What is include role in Ansible?

Roles let you automatically load related vars, files, tasks, handlers, and other Ansible artifacts based on a known file structure. After you group your content in roles, you can easily reuse them and share them with other users.

How do I run multiple roles in Ansible?

Step 1 — Navigate to /etc/ansible/roles directory and create the roles for prerequisites, MongoDB and NodeJS. You should now see three roles in your 'roles' directory. Step 2 — Write main. yml for prerequisites which installs Git.

How can variables be defined within a role in Ansible?

Roles allow you to call a set of variables, tasks, and handlers by simply specifying a defined role. Roles require the use of a defined file structure in order to work. Per the Ansible documentation, that structure looks like this… Roles are really just a way to split up your playbook into smaller reusable parts.


1 Answers

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

like image 198
masu Avatar answered Sep 24 '22 00:09

masu