Escaping double curly braces in Ansible

前端 未结 10 1153
我寻月下人不归
我寻月下人不归 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:13

    I was unable to get @Ben's answer to work (shell: !unsafe ...)

    What follows here is a complete (and working!) answer to the OP's question, updated for Ansible >2.0

    ---
    # file: play.yml
    
    - hosts: localhost
      connection: local
      gather_facts: no
      vars:
        # regarding !unsafe, please see:
        # https://docs.ansible.com/ansible/latest/user_guide/playbooks_advanced_syntax.html
        #
        - NetworkSettings_IPAddress: !unsafe "{{.NetworkSettings.IPAddress}}"
      tasks:
        - shell: "docker inspect --format '{{NetworkSettings_IPAddress}}' instance1"
          register: out
        - debug: var="{{item}}"                                                                                                   
          with_items:                                                                                                             
            - out.cmd                                                                                                             
            - out.stdout                                                                                                          
    

    outputs: ([WARNINGS] removed)

    # ansible-playbook play.yml
    PLAY [localhost] ***************************************************************
    
    TASK [shell] *******************************************************************
    changed: [localhost]
    
    TASK [debug] *******************************************************************
    ok: [localhost] => (item=out.cmd) => {
        "item": "out.cmd", 
        "out.cmd": "docker inspect --format '{{.NetworkSettings.IPAddress}}' instance1"
    }
    ok: [localhost] => (item=out.stdout) => {
        "item": "out.stdout", 
        "out.stdout": "172.17.0.2"
    }
    
    PLAY RECAP *********************************************************************
    localhost                  : ok=2    changed=1    unreachable=0    failed=0   
    

    # ansible --version | head -1
    ansible 2.6.1
    

提交回复
热议问题