Ansible不转义Windows路径的第一个参数 [英] Ansible not escaping windows path first argument

查看:183
本文介绍了Ansible不转义Windows路径的第一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的剧本的额外参数中带有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 as c:\\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).

  1. 不要使用debug模块来调试与数据有关的事物,它会处理字符串以使其可打印.

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

相反,将copycontent参数一起使用,并检查文件中的输出:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆