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
替换,实际上,您在屏幕上看到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
(和其他转义序列)中观察到相同的现象.
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.
相反,将copy
与content
参数一起使用,并检查文件中的输出:
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屋!