Ansible: playbook calling Role in a directory that is in the roles directory

Question:

I would like to shape my directory structure of my ansible roles and playbooks.

Currently I have a directory structure like.

I would like to be able to call upon the role java_role1 in the play deploy_fun_java_stuff.yml

I can call

but I cannot call (I’ve tried multiple ways). Is this possible?


What I really want to accomplish is to be able to structure my plays in an orderly fashion along with my roles.
I will end up with a large number of both roles and plays I would like to organize them.

I can handle this with a separate ansible.cfg file for each play directory but I cannot add those cfg files to ansible tower (So I’m looking for an alternate solution).

Answer:

I think the problem is that you need to set the relative path properly. Ansible first applies the given path relative to the called playbooks directory, then looks in the current working path (from which you are executing the ansible-playbook command) and finally checks in /etc/ansible/roles, so instead of { role: java/java_role1 } in your dir structure you could use { role: ../../roles/java/java_role1 } or { role: roles/java/java_role1 }. Yet another option would be to configure the paths in which ansible is looking for roles. For that you could set the roles_path inside your projects ansible.cfg as described in the Ansible docs.

Based on your example:

Dir tree:

To test it, the play would include java_role1 and role1.

plays/java_plays/java.yml:

For testing purposes these roles simply print a debug msg.

role1/tasks/main.yml:


The dev hosts file simply sets localhost to the java group. Now I can use the playbook:

Now doing the same when you use { role: ../../roles/java/java_role1 } and { role: ../../roles/role1 } your log output inside the TASK brackets would show the whole relative path instead of just the role name:

Leave a Reply