Using any custom character for commenting in Ansible comment filter ({{ … | comment }})
I needed to have comment in a PHP ini file where the prefix for commenting is `;`, This is something I didn’t find in the documentation for the comment filter, so I looked in the code for the comment (in my case: `/usr/lib/python2.7/site-packages/ansible/plugins/filter/core.py`)
def combine(*terms, **kwargs):
recursive = kwargs.get('recursive', False)
if len(kwargs) > 1 or (len(kwargs) == 1 and 'recursive' not in kwargs):
raise errors.AnsibleFilterError("'recursive' is the only valid keyword argument")
for t in terms:
if not isinstance(t, dict):
raise errors.AnsibleFilterError("|combine expects dictionaries, got " + repr(t))
if recursive:
return reduce(merge_hash, terms)
else:
return dict(itertools.chain(*map(iteritems, terms)))
def comment(text, style='plain', **kw):
# Predefined comment types
comment_styles = {
'plain': {
'decoration': '# '
},
'erlang': {
'decoration': '% '
},
'c': {
'decoration': '// '
},
'cblock': {
'beginning': '/*',
'decoration': ' * ',
'end': ' */'
},
'xml': {
'beginning': '<!--',
'decoration': ' - ',
'end': '-->;'
}
}
# Pointer to the right comment type
style_params = comment_styles[style]
if 'decoration' in kw:
prepostfix = kw['decoration']
else:
prepostfix = style_params['decoration']
And found the `decoration` keyword (which does not mention in the documentations), so I tried using it like this,
{{ ansible_managed | comment(decoration='; ') }}
And it works 🙂
; ; This file is managed by Ansible. ; ; template: /vagrant/provisioning/roles/backend/templates/php/xdebug.ini.j2 ; date: 2017-05-24 12:32:27 ; user: rabin ; host: localhost ;
Thanks! Great find. Was looking for it to deploy the customized .vimrc files, which use the double quote for the comments
Seems like this option is now documented 🙂