Cognitive
Cognitive

Reputation: 1

Ansible/Jinja2 nested escaping

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.

Upvotes: 0

Views: 237

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68294

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
                     }}"

Upvotes: 1

Related Questions