Ansible: print role name inside template

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

UPD 2: @sivel noticed much easier way to do it: {{ role_path | basename }}. Seems that whatever you are trying to do, already there is a jinja filter to do it. template: dest=/etc/mysql/my.cnf content="{{ 'mysql' | make_config_fast_reliable_and_secure }}" :))

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: Creative Commons Attribution-ShareAlike License Share: share on reddit share on facebook share on twitter