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

Fix fileglob when using 'file*' vs 'stuff/file*' (#68945) #69002

Merged
merged 1 commit into from May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/fileglob_fixes.yml
@@ -0,0 +1,2 @@
bugfixes:
- deal with cases in which just a file is pased and not a path with directories, now fileglob correctly searches in 'files/' subdirs.
18 changes: 16 additions & 2 deletions lib/ansible/plugins/lookup/fileglob.py
Expand Up @@ -58,8 +58,22 @@ def run(self, terms, variables=None, **kwargs):
ret = []
for term in terms:
term_file = os.path.basename(term)
dwimmed_path = self.find_file_in_search_path(variables, 'files', os.path.dirname(term))
if dwimmed_path:
found_paths = []
if term_file != term:
found_paths.append(self.find_file_in_search_path(variables, 'files', os.path.dirname(term)))
else:
# no dir, just file, so use paths and 'files' paths instead
if 'ansible_search_path' in variables:
paths = variables['ansible_search_path']
else:
paths = [self.get_basedir(variables)]
for p in paths:
found_paths.append(os.path.join(p, 'files'))
found_paths.append(p)

for dwimmed_path in found_paths:
globbed = glob.glob(to_bytes(os.path.join(dwimmed_path, term_file), errors='surrogate_or_strict'))
ret.extend(to_text(g, errors='surrogate_or_strict') for g in globbed if os.path.isfile(g))
if ret:
break
return ret
1 change: 1 addition & 0 deletions test/integration/targets/fileglob/aliases
@@ -0,0 +1 @@
shippable/posix/group1
@@ -0,0 +1 @@
in files subdir adjacent to play
@@ -0,0 +1 @@
in play adjacent subdir of files/
13 changes: 13 additions & 0 deletions test/integration/targets/fileglob/find_levels/play.yml
@@ -0,0 +1,13 @@
- hosts: localhost
gather_facts: false
vars:
expected:
play_adj: ajectent to play
play_adj_subdir: in files subdir adjacent to play
somepath/play_adj_subsubdir: in play adjacent subdir of files/
in_role: file in role
otherpath/in_role_subdir: file in role subdir
tasks:
- name: Import role lookup
import_role:
name: get_file
1 change: 1 addition & 0 deletions test/integration/targets/fileglob/find_levels/play_adj.txt
@@ -0,0 +1 @@
ajectent to play
@@ -0,0 +1 @@
file in role
@@ -0,0 +1 @@
file in role subdir
@@ -0,0 +1,10 @@
- name: show file contents
debug:
msg: '{{ q("fileglob", seed + ".*") }}'
register: found

- name: did we get right one?
assert:
that:
- found['msg'][0].endswith(seed + '.txt')
- q('file', found['msg'][0])[0] == expected[seed]
9 changes: 9 additions & 0 deletions test/integration/targets/fileglob/runme.sh
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -eux

# fun multilevel finds
for seed in play_adj play_adj_subdir somepath/play_adj_subsubdir in_role otherpath/in_role_subdir
do
ansible-playbook find_levels/play.yml -e "seed='${seed}'" "$@"
done