How to print caller role name from within another role?
Playbook:
- name: print calling role name
hosts: all
tasks:
- import_role:
name: role2
tasks_from: update
role2/tasks/update.yml:
- name: role 2
import_role:
name: role1
tasks_from: update
vars:
role_scope: "{{ role_name }}"
role1/tasks/update.yml:
- name: role 1
debug:
msg: "Calling role: {{ role_scope }}"
Current output
Calling role: role1
Expected output
Calling role: role2
As pointed in the special variables page, there is a variable
ansible_parent_role_namesthat contains a list of all the ancestor roles.So, the solution is to use it in
role1:And if you want it to work whatever it is called by another role or not, you could do:
This way, it will answer
When called via
role2, butwhen calling
role1directly in the playbook, for example.