好的。所以我有一个 ansible 剧本,如果在我循环的列表中找到特定值,我希望执行一个任务。任务看起来像这样:
- name: check to see if reg values have already been added
set_fact:
regpres: true
loop: "{{ reg.value }}"
when: item is search('-Dtaxlink.log.location=C:\taxlink\logs')
但是,当我运行此任务时,出现以下错误:
"msg": "The conditional check 'item is search('-Dtaxlink.log.location=C:\\taxlink\\logs')' failed. The error was: bad escape \\l
我什至尝试将任务中的反斜杠加倍,这使得它看起来像这样:
- name: check to see if reg values have already been added
set_fact:
regpres: true
loop: "{{ reg.value }}"
when: item is search('-Dtaxlink.log.location=C:\\taxlink\\logs')
我几乎再次遇到了完全相同的错误:
"msg": "The conditional check 'item is search('-Dtaxlink.log.location=C:\\\\taxlink\\\\logs')' failed. The error was: bad escape \\l
谁能帮我弄清楚这是怎么回事?
已经回答了。不玩这个游戏。/
search()
测试接受正则表达式作为参数,而不是纯字符串。用 4 个反斜杠转义是有效的:
# playbook.yaml
---
- hosts: localhost
gather_facts: false
vars:
reg:
value:
- -Dtaxlink.log.location=C:\taxlink\logs
- -Dtaxlink.log.location=D:\taxlink\logs
- -Dtaxlink.log.location=E:\taxlink\logs
- -Dtaxlink.log.location=C:\\taxlink\\logs
tasks:
- name: check to see if reg values have already been added
set_fact:
regpres: true
loop: "{{ reg.value }}"
when: item is search('-Dtaxlink.log.location=C:\\\\taxlink\\\\logs')
- name: Output the registered variable
debug:
var: regpres
$ ansible-playbook playbook.yaml
PLAY [localhost] *******************************************************************************************************************************************************************************************************************************
TASK [check to see if reg values have already been added] **************************************************************************************************************************************************************************************
ok: [localhost] => (item=-Dtaxlink.log.location=C:\taxlink\logs)
skipping: [localhost] => (item=-Dtaxlink.log.location=D:\taxlink\logs)
skipping: [localhost] => (item=-Dtaxlink.log.location=E:\taxlink\logs)
skipping: [localhost] => (item=-Dtaxlink.log.location=C:\\taxlink\\logs)
TASK [Output the registered variable] **********************************************************************************************************************************************************************************************************
ok: [localhost] => {
"regpres": true
}
PLAY RECAP *************************************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0