Ansible: print role name inside template

UPDATE 3: Starting from ansible 2.8 there is a special variable role_name.

UPDATE 2: @sivel noticed much easier way to do it: {{ role_path | basename }}.

UPDATE: I created a feature request, asking for ansible guys to make special variable ansible_role: #25447.

Ansible guys suppose to use {{ ansible_managed }} inside templates to indicate where it came from. Unfortunately, this variable contains date and time, so it is changed on every playbook run. That breaks --check mode.

I prefer to include role name in template, so you can see where it came from. If you hard-code it, after refactoring that will become broken. Here is how you can generate it dynamicaly:

# Generated by ansible role: {{ role_path | regex_replace('^.*/([^/]+)$', '\\1') }}

It’s a bit clumsy, but it will never break.



License: This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License Share: share on reddit share on hacker news share on facebook share on twitter share on linkedin share on slashdot

No comments

You today

Comments are closed