Skip to content

ansible.builtin.search test fails with "bad escape" error if pattern contains backslash anywhere #1086

Open
@youngturk2

Description

@youngturk2

Summary

consider the following task that I am trying to run in ansible:

- name: check to see if reg values have already been added
  set_fact:
    regpres: "{{ item }}"
  loop: "{{ reg.value }}"
  when: item is search('-Dtaxlink.log.location=C:\taxlink\logs')

I am trying to loop through a list and only want the task to run if an item in the list contains the string I have specified. While I have used the search test many, many times before, I have never used it with a pattern that contained a backslash, and when I attempt to run the above task in ansible, I get the below error:

"msg": "The conditional check 'item is search('-Dtaxlink.log.location=C:\\taxlink\\logs')' failed. The error was: bad escape \\l at position 32

I even tried doubling up the backslashes in the pattern to see if that would fix the issue:

- name: check to see if reg values have already been added
  set_fact:
    regpres: "{{ item }}"
  loop: "{{ reg.value }}"
  when: item is search('taxlink.log.location=C:\\taxlink\\logs')

but I received an almost identical error:

"msg": "The conditional check 'item is search('taxlink.log.location=C:\\\\taxlink\\\\logs')' failed. The error was: bad escape \\l at position 33

when I change the conditional so that the pattern will match a string in the list exactly and use "==", the task work perfectly:

- name: check to see if reg values have already been added
  set_fact:
    regpres: "{{ item }}"
  loop: "{{ reg.value }}"
  when: item == '-Dtaxlink.log.location=C:\taxlink\logs'

So I am lead to conclude that this is an issue with either the ansible.builtin search test and backslashes or tests in general and backslashes.

Issue Type

Bug Report

Component Name

search

Ansible Version

$ ansible --version
ansible [core 2.15.8]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/ubuntu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/ubuntu/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True

Configuration

# if using a version older than ansible-core 2.12 you should omit the '-t all'
$ ansible-config dump --only-changed -t all
CONFIG_FILE() = /etc/ansible/ansible.cfg
DEFAULT_VAULT_PASSWORD_FILE(env: ANSIBLE_VAULT_PASSWORD_FILE) = /home/ubuntu/.vp

OS / Environment

Ubuntu 22.04.3 LTS

Steps to Reproduce

- name: check to see if reg values have already been added
  set_fact:
    regpres: "{{ item }}"
  loop: "{{ reg.value }}"
  when: item is search('-Dtaxlink.log.location=C:\taxlink\logs')

or

- name: check to see if reg values have already been added
  set_fact:
    regpres: "{{ item }}"
  loop: "{{ reg.value }}"
  when: item is search('-Dtaxlink.log.location=C:\\taxlink\\logs')

Try running either of the above tasks in a playbook

Expected Results

I would expect the search test to complete without error

Actual Results

fatal: [ouplift11a.onpvertexinc.com -> localhost]: FAILED! => {
    "msg": "The conditional check 'item is search('-Dtaxlink.log.location=C:\\taxlink\\logs')' failed. The error was: bad escape \\l at position 32\n\nThe error appears to be in '/home/ubuntu/ansible/playbook/roles/installconnect/tasks/hold.yml': line 15, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: check to see if reg values have already been added\n  ^ here\n"
}
fatal: [ouplift11b.onpvertexinc.com -> localhost]: FAILED! => {
    "msg": "The conditional check 'item is search('-Dtaxlink.log.location=C:\\taxlink\\logs')' failed. The error was: bad escape \\l at position 32\n\nThe error appears to be in '/home/ubuntu/ansible/playbook/roles/installconnect/tasks/hold.yml': line 15, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: check to see if reg values have already been added\n  ^ here\n"
}

Code of Conduct

  • I agree to follow the Ansible Code of Conduct

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingnew_contributorThis PR is the first contribution by a new community member.

Type

No type

Projects

Status

🆕 Triage

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions