Ansible not escaping windows path first argument

2019-02-28 21:09发布

问题:

I have playbook with windows path name in the extra arguments. first argument not escaping the drive letter and slash.

ansible-playbook d.yaml  --extra-vars "ainstalldir=c:\\test stagedir=D:\packages outdir=d:\output\log"

TASK [print inpurt arguments] ********************************************************************************************************
ok: [127.0.0.1] => {
    "msg": "installdir=c:\test, stragedir=D:\\packages, outdir=d:\\output\\log"
}

installdir prints as c:\test, I expect it should print as c:\\test

Here is my playbook.

---
- name: test command line arguments
  connection: local
  hosts: 127.0.0.1
  gather_facts: false
  vars:
    installdir: "{{ ainstalldir }}"
    stagedir: "{{ stagedir }}"
    outdir: "{{ outdir }}"

  tasks:
  - name: print inpurt arguments
    debug:
      msg="installdir={{ installdir }}, stragedir={{ stagedir }}, outdir={{ outdir }}"

Any idea how to resolve this issue?

回答1:

installdir prints as c:\test, I expect it should print as c:\\test

installdir contains: c : tab e s t.

tab is replaced with \t in the debug module output and in effect you see c:\test on the screen.

Other characters starting with backslash in your example (\p, \o, \l) do not have special meaning, so they are treated as two character strings; but you'd observe the same phenomenon with \n (and other escape sequences).


  1. Don't use debug module to debug things concerned with data, it processes strings to make them printable.

    Instead, use copy with content parameter and check the output in a file:

    - copy:
        content: |-
          installdir={{ installdir }}
          stragedir={{ stagedir }}
          outdir={{ outdir }}
        dest: ./result.txt
    

    (remember you could/should use hexdump to verify what's really inside).

  2. Use:

    ansible-playbook d.yaml --extra-vars "ainstalldir=c:\\\test stagedir=D:\\\packages outdir=d:\\\output\\\log"
    

    or

    ansible-playbook d.yaml --extra-vars 'ainstalldir=c:\\test stagedir=D:\\packages outdir=d:\\output\\log'
    

    Backslashes in double- and single quotes are interpreted differently by shell (see for example this question).



标签: ansible