Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
youngturk2 opened this issue Jan 28, 2024 · 3 comments
Assignees
Labels
bug Something isn't working new_contributor This PR is the first contribution by a new community member.

Comments

@youngturk2
Copy link

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
@ansibot ansibot added bug Something isn't working needs_triage Needs a first human triage before being processed. labels Jan 28, 2024
@ansibot
Copy link
Contributor

ansibot commented Jan 28, 2024

Files identified in the description:

None

If these files are incorrect, please update the component name section of the description or use the component bot command.

@mkrizek mkrizek removed the needs_triage Needs a first human triage before being processed. label Jan 30, 2024
@sivel
Copy link
Member

sivel commented Jan 30, 2024

This is effectively expected, and ultimately you need to use \\\\. There are multiple layers to deal with, each potentially requiring their own escaping.

The string gets interpreted multiple times First by yaml, then by python, and finally by jinja2 as part of it's variable. Because it is processed by both python and jinja2, the backslash escaped characters get unescaped twice. This means that you normally have to use four backslashes to escape that.

I assumed we had documentation that describes this, but I cannot find it. And actually as I am typing this, I remembered this issue: ansible/ansible#82523

I'll transfer this issue over to the docs repo.

@sivel sivel transferred this issue from ansible/ansible Jan 30, 2024
@ansible-documentation-bot ansible-documentation-bot bot added the new_contributor This PR is the first contribution by a new community member. label Jan 30, 2024
@ansible-documentation-bot
Copy link
Contributor

Thanks for your Ansible docs contribution! We talk about Ansible documentation on matrix at #docs:ansible.im and on libera IRC at #ansible-docs if you ever want to join us and chat about the docs! We meet there on Tuesdays (see the Ansible calendar) and welcome additions to our weekly agenda items - scroll down to find the upcoming agenda and add a comment to put something new on that agenda.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working new_contributor This PR is the first contribution by a new community member.
Projects
Status: 🆕 Triage
Development

No branches or pull requests

4 participants