Ok. So I have an ansible playbook where I want a task to execute if a particular value is found in the list I am looping through. The task looks like this:
- 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')
When I run this task, however, I get the following error:
"msg": "The conditional check 'item is search('-Dtaxlink.log.location=C:\\taxlink\\logs')' failed. The error was: bad escape \\l
I even tried doubling up the backslashes in the task, which made it look like this:
- 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')
And I pretty much got the exact same error again:
"msg": "The conditional check 'item is search('-Dtaxlink.log.location=C:\\\\taxlink\\\\logs')' failed. The error was: bad escape \\l
Can anyone help me figure out what is going on here?
Already answered. Not playing this game.
search()
test accepts a regex as argument, not a plain string. Escaping with 4 backslashes works:
# 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