变量中的值和带反斜杠的字符串可以组合在 ansible 中吗? [英] Can a value in a variable and a string with backslashes be combined in ansible?

查看:23
本文介绍了变量中的值和带反斜杠的字符串可以组合在 ansible 中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ansible playbook,其中我从命令中传递了一个变量.我正在尝试向其附加一个 Windows 文件夹路径.我能够找出的一种方法是将路径添加到另一个变量,然后加入这两个变量.我想知道是否可以避免该变量并将路径放置如下:"{{ variable2 }} \build\dist\package\ui.msi"

I have an ansible playbook in which one variable I am passing from the command. I am trying to append a windows folder path to it. One way I am able to find out is to add the path to another variable and then join the two variable. I would like to know if it is possible to avoid the variable and put the path like this: "{{ variable2 }} \build\dist\package\ui.msi"

变量 1 的值为 "d:\install"var_build_file_name 由用户输入variable2 由 variable1 和 var_build_number 组合而成.

variable1 has value "d:\install" var_build_file_name is entered by the user variable2 is formed by combining variable1 and var_build_number.

这是剧本中的实际内容:

This is the actual content in the playbook which works:

  vars:
    installerFolder: "{{ UploadFolder }}{{ var_build_file_name | regex_replace('(\\.zip)','\') }}"
    packagePath: '\build\dist\package\UI.msi'

  - name: Install new version.
    debug:
      msg: "{{ installerFolder }}{{ packagePath }}"

这是剧本命令:

ansible-playbook Install.yml -i ../inventory/hosts.ini --extra-vars "target=servername var_build_file_name=16.3.0.zip UploadFolder=D:\Install\\"

我得到的输出是:

"msg": "D:\\Install\\16.3.0\\build\\dist\\package\\UI.msi"

"msg": "D:\\Install\\16.3.0\\build\\dist\\package\\UI.msi"

在上面的输出中,为什么显示的是两个反斜杠而不是一个?

In the above output, how come two backslashes are showing instead of one?

可以吗

msg: "{{ installerFolder }} '\build\dist\package\UI.msi' "

我尝试了很多组合,但上面的反斜杠没有正确转义.如果不可能,那么有人可以说明原因.

I have tried many combinations but the backslashes are not getting properly escaped for above. If it is not possible then can someone enlighten on the reason.

谢谢.

推荐答案

"msg": "D:\\Install\\16.3.0\\build\\dist\\package\\UI.msi"

是debug模块的输出,是一个JSON字符串,所以每一个\都被转义了.
此处 msg 的实际值是 D:\Install\16.3.0\build\dist\package\UI.msi,正如您所期望的那样.

It is the output of debug module, which is a JSON string, so every \ is escaped.
The actual value of the msg here is D:\Install\16.3.0\build\dist\package\UI.msi, as you expect it.

你绝对可以使用这个语法:msg: '{{ installerFolder }}\build\dist\package\UI.msi'

And you can definitely use this syntax: msg: '{{ installerFolder }}\build\dist\package\UI.msi'

这篇关于变量中的值和带反斜杠的字符串可以组合在 ansible 中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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