From 74a204e6f144f3eabd6384bbb665b6afd69117c3 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Wed, 2 Mar 2022 22:16:47 +0100 Subject: [PATCH] first_found: allow spaces in names (#77141) * first_found: allow spaces in names Fixes #77136 --- .../fragments/77136-first_found-spaces-in-names.yml | 2 ++ lib/ansible/plugins/lookup/first_found.py | 8 ++------ .../lookup_first_found/files/vars file spaces.yml | 1 + .../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 00000000000..34e30d3173a --- /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 20d22510d1f..2440a363ee9 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 00000000000..790bc26c9dd --- /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 e85f4f27ad2..9aeaf1d1ed5 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