
我有一個任務:
- name: copy files
copy:
src: "c:\\path\\to\\dir{{ item | replace('/','\\') }}"
dest: "/tmp/{{ item }}"
with_items:
- "{{ paths }}"
我也嘗試過:replace(\"/\",\"\\\")
但replace(\"/\",\"\\\\\")
沒有任何效果,有什麼幫助嗎?
paths
包含:project/folder1/bin
、project/folder2/bin
等...
答案1
您可以透過定義一個以反斜線作為其值的變數來避免引號問題:
- name: copy files
copy:
src: "c:\\path\\to\\dir\\{{ item | replace('/', backslash) }}"
dest: "/tmp/{{ item }}"
vars:
backslash: "\\"
with_items:
- "{{ paths }}"