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
with_first_found fails to find a file if an undefined variable is used in the list since ansible 2.9 #70772
Comments
Files identified in the description: If these files are incorrect, please update the |
Files identified in the description: If these files are incorrect, please update the |
The issue here, is in filtering out untemplated terms in the search. We only account for strings in the terms in this code path. The following diff should resolve the issue: diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py
index 5034bbc862..373f47e082 100644
--- a/lib/ansible/executor/task_executor.py
+++ b/lib/ansible/executor/task_executor.py
@@ -19,6 +19,7 @@ from ansible import constants as C
from ansible.errors import AnsibleError, AnsibleParserError, AnsibleUndefinedVariable, AnsibleConnectionFailure, AnsibleActionFail, AnsibleActionSkip
from ansible.executor.task_result import TaskResult
from ansible.executor.module_common import get_action_args_with_defaults
+from ansible.module_utils.common._collections_compat import Mapping
from ansible.module_utils.parsing.convert_bool import boolean
from ansible.module_utils.six import iteritems, string_types, binary_type
from ansible.module_utils.six.moves import xrange
@@ -223,8 +224,17 @@ class TaskExecutor:
loop_terms = listify_lookup_plugin_terms(terms=self._task.loop, templar=templar, loader=self._loader, fail_on_undefined=fail,
convert_bare=False)
+
if not fail:
- loop_terms = [t for t in loop_terms if not templar.is_template(t)]
+ intermediate = []
+ for term in loop_terms:
+ if isinstance(term, Mapping) and 'files' in term:
+ term['files'] = [t for t in term['files'] if not templar.is_template(t)]
+ intermediate.append(term)
+ else:
+ if not templar.is_template(term):
+ intermediate.append(term)
+ loop_terms = intermediate
# get lookup
mylookup = self._shared_loader_obj.lookup_loader.get(self._task.loop_with, loader=self._loader, templar=templar) If anyone is interested in testing this further, and creating tests to validate it, please feel free to do so. |
Hey @afunix Are you able to test the fix and provide some feedback ? |
@afunix I eventually took the patch and did a PR. |
I confirm the change fixes my issue. |
needs_verified (first_found input was revamped and that might have fixed this issue) |
I can still reproduce the issue on ansible 2.12.5 (MacOSX, installed with brew) |
I'm also still hitting this issue and wondering if there is a fix for this |
We don't have any files in vars/ for this role. If the 'distribution' facts are not collected it would fail with "No file was found when using first_found" due to this open ansible bug[1]. [1] ansible/ansible#70772 Change-Id: I43f18466a064234cb62c5b6d7f9a2701dce9cb04
* Update tripleo-ansible from branch 'master' to 4a32ee47f30a24e668868a90c176a79ed9b0ec1d - Merge "Drop gather variables task tripleo_keystone_resources" - Drop gather variables task tripleo_keystone_resources We don't have any files in vars/ for this role. If the 'distribution' facts are not collected it would fail with "No file was found when using first_found" due to this open ansible bug[1]. [1] ansible/ansible#70772 Change-Id: I43f18466a064234cb62c5b6d7f9a2701dce9cb04
We don't have any files in vars/ for this role. If the 'distribution' facts are not collected it would fail with "No file was found when using first_found" due to this open ansible bug[1]. [1] ansible/ansible#70772 Change-Id: I43f18466a064234cb62c5b6d7f9a2701dce9cb04 (cherry picked from commit e205948)
This is my work-around, instead of sth like this:
instead, you can use:
|
^ weird enough, this does work ... but that makes me thing the issue is how we look at how the terms get passed into the lookup itself vs the processing in the lookup |
This line seems to be the problem: If i remove it 'everything works', otherwise it removes the lines that require templating from being sent to the plugin, which then gets Going to try to figure out why that was added |
The issue was that the 'undefined' var forced the whole item to be removed in task_executor, so the form that had everything in 'one place' ended up empty, the 2nd form @ruoshan showed, 'worked'TM because only the item with the undefined variable was removed. The code was iterating on the top level list of dicts, vs list of files in dict key named 'files', so it removed the full dict. |
@bcoca I was wrong, that work-around has other issue. if the files in both |
That is the nature of lists, each item is still processed, you would have to reverse order for that case to match the behavior you want. This is not something that was ever documented/advertised as supported (list of dicts), not sure if i should make it an error, document the behavior or change it to also break on first found. |
@roushan hmm, my tests seem to show it working as the last option above, its 'first found' |
SUMMARY
with_first_found fails "No file was found when using first_found. ..." even if a file matches search conditions, when an undefined variable is used in files parameter.
This is started after I've upgraded from ansible 2.8 to 2.9, so I suppose it is a bug.
ISSUE TYPE
COMPONENT NAME
first_found
ANSIBLE VERSION
CONFIGURATION
OS / ENVIRONMENT
Host: macOS 10.15.5
Target: CentOS 7.8
STEPS TO REPRODUCE
playbook.yml
roles/test/tasks/main.yml
roles/test/vars/CentOS-7.yml
EXPECTED RESULTS
ansible-playbook is able to find CentOS-7.yml when running against CentOS 7.8 system, since the file exists.
ACTUAL RESULTS
ansible-playbook cannot find CentOS-7.yml when running against CentOS 7.8 system.
The issue goes away once I remove "{{ansible_lsb.id}}-{{ansible_lsb.major_release}}.yml" from files.
Checked ansible -m setup for the host and ansible_lsb is empty.
The text was updated successfully, but these errors were encountered: