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

Don't use the task for a cache, return a special cache var #47243

Merged
merged 6 commits into from
Oct 18, 2018

Conversation

sivel
Copy link
Member

@sivel sivel commented Oct 17, 2018

SUMMARY

This PR addresses an issue where looping over a hostvar, with a templated delegate_to would create a loop cache on the task for the last host in the loop. Fixes #47207

The original problem that was being solved was the following:

- command: whoami
  delegate_to: "{{ item }}"
  with_random_choice: "{{ ansible_play_hosts }}"

Because we process the loop in VariableManager._get_delegated_vars and in TaskExecutor._get_loop_items that loop could produce different results, causing ansible_delegated_vars to not exist for the host later selected in TaskExecutor._get_loop_items

We solved that by caching the resultant loop as task.loop. The problem with that is when the variable being looped is different per host.

This fix allows the original case to work, as well as the case of:

- command: whoami
  delegate_to: "{{ item }}"
  loop: "{{ some_per_host_list }}"
ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

lib/ansible/executor/task_executor.py
lib/ansible/vars/manager.py

ANSIBLE VERSION
2.7
2.8
ADDITIONAL INFORMATION

Also, not mentioned above is that an inventory was supplied to some other tests that were skipping as a result of the inventory not matching.

@sivel sivel requested review from jimi-c and bcoca October 17, 2018 20:20
if loop_cache is not None:
# _ansible_loop_cache may be set in `get_vars` when calculating `delegate_to`
# to avoid reprocessing the loop
items = copy.copy(loop_cache)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't we need a deepcopy here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, actually, I can remove that all together. I had added it when I was removing it from _job_vars.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

@ansibot
Copy link
Contributor

ansibot commented Oct 17, 2018

Hi @sivel, thank you for submitting this pull-request!

click here for bot help

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Oct 17, 2018
@jborean93 jborean93 removed the needs_triage Needs a first human triage before being processed. label Oct 18, 2018
@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed core_review In order to be merged, this PR must follow the core review workflow. labels Oct 18, 2018
@sivel sivel merged commit 77d32b8 into ansible:devel Oct 18, 2018
sivel added a commit to sivel/ansible that referenced this pull request Oct 18, 2018
…ar (ansible#47243)

* Don't use task to cache loop results, use hostvars. Fixes ansible#47207

* Avoid a race condition, supply _ansible_loop_cache through get_vars directly

* Add tests

* Add changelog fragment

* Remove unnecessary copy

* Remove unnecessary host from _get_delegated_vars signature.
(cherry picked from commit 77d32b8)

Co-authored-by: Matt Martz <matt@sivel.net>
abadger pushed a commit that referenced this pull request Oct 22, 2018
…ar (#47243)

* Don't use task to cache loop results, use hostvars. Fixes #47207

* Avoid a race condition, supply _ansible_loop_cache through get_vars directly

* Add tests

* Add changelog fragment

* Remove unnecessary copy

* Remove unnecessary host from _get_delegated_vars signature.
(cherry picked from commit 77d32b8)

Co-authored-by: Matt Martz <matt@sivel.net>
Tomorrow9 pushed a commit to Tomorrow9/ansible that referenced this pull request Dec 4, 2018
…7243)

* Don't use task to cache loop results, use hostvars. Fixes ansible#47207

* Avoid a race condition, supply _ansible_loop_cache through get_vars directly

* Add tests

* Add changelog fragment

* Remove unnecessary copy

* Remove unnecessary host from _get_delegated_vars signature
@ansible ansible locked and limited conversation to collaborators Jul 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'with_dict' not working correctly when used in combination with a variable as input for 'delegate_to'
5 participants