Can a value in a variable and a string with backsl

2019-09-15 20:18发布

问题:

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"

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 }}"

And this is the playbook command:

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

The output I am getting is:

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

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

Is it possible to do

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.

Thanks.

回答1:

"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.

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