0

Is there any way to have nested escaping of jinja2 template?

  replace:
    path: /etc/my_file
    regexp: '^my_var = ""'
    replace: !unsafe "my_var = {{getv '/{{ variable}}/my_dir/my_file'}}"

I'd like to replace my_var = "" with my_var = {{getv '/staging/my_dir/my_file'}}

In this case, {{getv}} shouldn't be templated (external parenthesis), but {{variable}} inside (internal parenthesis) should be.

  • "Whenever you have problems with conflicting characters in Ansible, a rule of thumb is to output them as a string in a Jinja expression." see: https://stackoverflow.com/a/32283447/2553262 – Stefan Wegener Oct 21 '19 at 07:38

1 Answers1

1

It's possible to concatenate the replace string. For example

  vars:
    lbrackets: "{{ '{{' }}"
    rbrackets: "{{ '}}' }}"
    quote: "'"

  tasks:
    - replace:
        path: /etc/my_file
        regexp: '^my_var = ""(.*)$'
        replace: "{{ 'my_var = ' ~
                     lbrackets ~ 'getv ' ~ quote ~
                     '/staging/my_dir/my_file' ~ quote ~ rbrackets
                     }}"
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63