From 17836f8e651c5ed196c4d9b358469381f12e5399 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Tue, 15 Mar 2022 19:04:15 +0100 Subject: first_found: allow spaces in names (#77141) (#77207) * first_found: allow spaces in names Fixes #77136 (cherry picked from commit 74a204e6f144f3eabd6384bbb665b6afd69117c3) --- changelogs/fragments/77136-first_found-spaces-in-names.yml | 2 ++ lib/ansible/plugins/lookup/first_found.py | 8 ++------ .../targets/lookup_first_found/files/vars file spaces.yml | 1 + test/integration/targets/lookup_first_found/tasks/main.yml | 10 ++++++++++ 4 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/77136-first_found-spaces-in-names.yml create mode 100644 test/integration/targets/lookup_first_found/files/vars file spaces.yml diff --git a/changelogs/fragments/77136-first_found-spaces-in-names.yml b/changelogs/fragments/77136-first_found-spaces-in-names.yml new file mode 100644 index 0000000000..34e30d3173 --- /dev/null +++ b/changelogs/fragments/77136-first_found-spaces-in-names.yml @@ -0,0 +1,2 @@ +bugfixes: + - first_found - fix to allow for spaces in file names (https://github.com/ansible/ansible/issues/77136) diff --git a/lib/ansible/plugins/lookup/first_found.py b/lib/ansible/plugins/lookup/first_found.py index 5d54959ec5..8a37a6898c 100644 --- a/lib/ansible/plugins/lookup/first_found.py +++ b/lib/ansible/plugins/lookup/first_found.py @@ -134,6 +134,7 @@ RETURN = """ elements: path """ import os +import re from jinja2.exceptions import UndefinedError @@ -144,18 +145,13 @@ from ansible.plugins.lookup import LookupBase def _split_on(terms, spliters=','): - - # TODO: fix as it does not allow spaces in names termlist = [] if isinstance(terms, string_types): - for spliter in spliters: - terms = terms.replace(spliter, ' ') - termlist = terms.split(' ') + termlist = re.split(r'[%s]' % ''.join(map(re.escape, spliters)), terms) else: # added since options will already listify for t in terms: termlist.extend(_split_on(t, spliters)) - return termlist diff --git a/test/integration/targets/lookup_first_found/files/vars file spaces.yml b/test/integration/targets/lookup_first_found/files/vars file spaces.yml new file mode 100644 index 0000000000..790bc26c9d --- /dev/null +++ b/test/integration/targets/lookup_first_found/files/vars file spaces.yml @@ -0,0 +1 @@ +foo: 1 diff --git a/test/integration/targets/lookup_first_found/tasks/main.yml b/test/integration/targets/lookup_first_found/tasks/main.yml index e85f4f27ad..9aeaf1d1ed 100644 --- a/test/integration/targets/lookup_first_found/tasks/main.yml +++ b/test/integration/targets/lookup_first_found/tasks/main.yml @@ -84,3 +84,13 @@ assert: that: - "hatethisformat == '/etc/hosts'" + +- name: test spaces in names + include_vars: "{{ item }}" + with_first_found: + - files: + - "{{ role_path + '/files/vars file spaces.yml' }}" + +- assert: + that: + - foo is defined -- cgit v1.2.1