Don't use the task for a cache, return a special cache var (#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
This commit is contained in:
Matt Martz 2018-10-18 15:25:43 -05:00 committed by GitHub
parent 9e2c02455a
commit 77d32b8f57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 10 deletions

View file

@ -9,6 +9,8 @@ ansible-playbook test_loop_control.yml -v "$@"
ansible-playbook test_delegate_to_loop_randomness.yml -v "$@"
ansible-playbook delegate_and_nolog.yml -v "$@"
ansible-playbook delegate_and_nolog.yml -i ../../inventory -v "$@"
ansible-playbook delegate_facts_block.yml -v "$@"
ansible-playbook delegate_facts_block.yml -i ../../inventory -v "$@"
ansible-playbook test_delegate_to_loop_caching.yml -i ../../inventory -v "$@"

View file

@ -0,0 +1,45 @@
- hosts: testhost,testhost2
gather_facts: false
vars:
delegate_to_host: "localhost"
tasks:
- set_fact:
gandalf:
shout: 'You shall not pass!'
when: inventory_hostname == 'testhost'
- set_fact:
gandalf:
speak: 'Run you fools!'
when: inventory_hostname == 'testhost2'
- name: works correctly
debug: var=item
delegate_to: localhost
with_dict: "{{ gandalf }}"
register: result1
- name: shows same item for all hosts
debug: var=item
delegate_to: "{{ delegate_to_host }}"
with_dict: "{{ gandalf }}"
register: result2
- debug:
var: result2.results[0].item.value
- assert:
that:
- result1.results[0].item.value == 'You shall not pass!'
- result2.results[0].item.value == 'You shall not pass!'
when: inventory_hostname == 'testhost'
- assert:
that:
- result1.results[0].item.value == 'Run you fools!'
- result2.results[0].item.value == 'Run you fools!'
when: inventory_hostname == 'testhost2'
- assert:
that:
- _ansible_loop_cache is undefined