How to escape backslash and double quote in Ansible (script module)

前端 未结 2 760
走了就别回头了
走了就别回头了 2021-01-04 20:18

I\'m very new to Ansible (2.x) and I am having trouble using the script module and passing parameters with double quotes and backslashes.

Assuming we have a set vari

2条回答
  •  一个人的身影
    2021-01-04 21:05

    You're very close. I think you want to set a variable, not a fact, and I would suggest you use the shell module instead of the script module. shell is more forgiving when it comes to escaping and quoting complex shell commands.

    ---
    - hosts: localhost
      vars:
        foo: test.yml
        arg: \( -name "{{ foo }}" \)
      tasks:
        - debug: var=arg
        - shell: find . {{ arg }}
          register: find
        - debug: var=find.stdout_lines
    

    And the output:

    $ ansible-playbook test.yml 
    
    PLAY [localhost] ***************************************************************
    
    TASK [debug] *******************************************************************
    ok: [localhost] => {
        "arg": "\\( -name \"test.yml\" \\)"
    }
    
    TASK [command] *****************************************************************
    changed: [localhost]
    
    TASK [debug] *******************************************************************
    ok: [localhost] => {
        "find.stdout_lines": [
            "./test.yml"
        ]
    }
    
    PLAY RECAP *********************************************************************
    localhost                  : ok=3    changed=1    unreachable=0    failed=0   
    

提交回复
热议问题