Ansible 不转义 Windows 路径第一个参数 [英] Ansible not escaping windows path first argument
问题描述
我在额外参数中有带有 Windows 路径名的剧本.第一个参数不转义驱动器号和斜线.
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 打印为 c:\test
,我希望它应该打印为 c:\\test
installdir prints as c:\test
, I expect it should print as c:\\test
这是我的剧本.
---
- 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?
推荐答案
installdir 打印为
c:\test
,我希望它应该打印为c:\\test
installdir prints as
c:\test
, I expect it should print asc:\\test
installdir
包含:c
:
tab e
s
t
.
tab 替换为 \t
在 debug
模块输出中,实际上你看到 c:\test
在屏幕上.
tab is replaced with \t
in the debug
module output and in effect you see c:\test
on the screen.
示例中以反斜杠开头的其他字符(\p
、\o
、\l
)没有特殊含义,因此它们被视为两个字符串;但是你会用 \n
(和其他 escape序列).
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).
不要使用
debug
模块来调试与数据有关的东西,它会处理字符串以使其可打印.
Don't use
debug
module to debug things concerned with data, it processes strings to make them printable.
相反,使用带有 content
参数的 copy
并检查文件中的输出:
Instead, use copy
with content
parameter and check the output in a file:
- copy:
content: |-
installdir={{ installdir }}
stragedir={{ stagedir }}
outdir={{ outdir }}
dest: ./result.txt
(请记住,您可以/应该使用 hexdump
来验证真正的内部内容).
(remember you could/should use hexdump
to verify what's really inside).
使用:
ansible-playbook d.yaml --extra-vars "ainstalldir=c:\\\test stagedir=D:\\\packages outdir=d:\\\output\\\log"
或
ansible-playbook d.yaml --extra-vars 'ainstalldir=c:\\test stagedir=D:\\packages outdir=d:\\output\\log'
shell 对双引号和单引号中的反斜杠的解释不同(参见例如这个问题).
Backslashes in double- and single quotes are interpreted differently by shell (see for example this question).
这篇关于Ansible 不转义 Windows 路径第一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!