summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Krizek <martin.krizek@gmail.com>2022-03-15 19:04:15 +0100
committerGitHub <noreply@github.com>2022-03-15 11:04:15 -0700
commit17836f8e651c5ed196c4d9b358469381f12e5399 (patch)
tree1eb91c5db074dc65a61359adc4f550b0fe28744a
parent2ff3ee75a506933dd341649107961d44399b6fce (diff)
downloadansible-17836f8e651c5ed196c4d9b358469381f12e5399.tar.gz
first_found: allow spaces in names (#77141) (#77207)
* first_found: allow spaces in names Fixes #77136 (cherry picked from commit 74a204e6f144f3eabd6384bbb665b6afd69117c3)
-rw-r--r--changelogs/fragments/77136-first_found-spaces-in-names.yml2
-rw-r--r--lib/ansible/plugins/lookup/first_found.py8
-rw-r--r--test/integration/targets/lookup_first_found/files/vars file spaces.yml1
-rw-r--r--test/integration/targets/lookup_first_found/tasks/main.yml10
4 files changed, 15 insertions, 6 deletions
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