Escaping double curly braces in Ansible

前端 未结 10 1138
我寻月下人不归
我寻月下人不归 2020-12-13 05:19

How to escape double curly braces in Ansible 1.9.2?

For instance, how can I escape double curly braces in the following shell command?

- name: Test 
         


        
10条回答
  •  一生所求
    2020-12-13 06:05

    I have a similar issue: i need to post a JSON doc made from a jinja2 template containing some go templates variables (yes, i know :-P), such as

    "NAME_TEMPLATE": %{{service_name}}.%{{stack_name}}.%{{environment_name}}

    Trying to fence this part of the template between

    {% raw %} ... {% endraw %}

    didn't work because there is some sort of magic in ansible which will run the template and variable substition twice (i'm not sure about that, but it definitively looks like this)

    You end up with "undefined variable service_name" when trying to use the template...

    So i ended up using a combination of !unsafe and {% raw %} ... {% endraw %} to define a fact that's later used in the template.

    - set_fact:
       __rancher_init_root_domain: "{{ rancher_root_domain }}"
       #!unsafe: try to trick ansible into not doing substitutions in that string, then use %raw% so the value won't substituted another time
       __rancher_init_name_template: !unsafe "{%raw%}%{{service_name}}.%{{stack_name}}.%{{environment_name}}{%endraw%}"
    
    - name: build a template for a project
      set_fact:
        __rancher_init_template_doc: "{{ lookup('template', 'templates/project_template.json.j2') }}"
    

    the template contains this:

        "ROOT_DOMAIN":"{{__rancher_init_root_domain}}",
        "ROUTE53_ZONE_ID":"",
        "NAME_TEMPLATE":"{{__rancher_init_name_template }}",
        "HEALTH_CHECK":"10000",
    

    and the output is ok:

    "NAME_TEMPLATE": "%{{service_name}}.%{{stack_name}}.%{{environment_name}}",
    

提交回复
热议问题