allow incremental update for vars in loop for set_fact/include_vars (#38302)

* fix set_fact/include_vars looping

* tests for all behaviours affected

* add tests with injection off/on
pull/38369/head
Brian Coca 7 years ago committed by Will Thames
parent 1b9879ccbf
commit 2afb1090b1

@ -605,10 +605,13 @@ class TaskExecutor:
failed_when_result = False failed_when_result = False
return failed_when_result return failed_when_result
if 'ansible_facts' in result and self._task.action not in ('set_fact', 'include_vars'): if 'ansible_facts' in result:
vars_copy.update(namespace_facts(result['ansible_facts'])) if self._task.action in ('set_fact', 'include_vars'):
if C.INJECT_FACTS_AS_VARS: vars_copy.update(result['ansible_facts'])
vars_copy.update(clean_facts(result['ansible_facts'])) else:
vars_copy.update(namespace_facts(result['ansible_facts']))
if C.INJECT_FACTS_AS_VARS:
vars_copy.update(clean_facts(result['ansible_facts']))
# set the failed property if it was missing. # set the failed property if it was missing.
if 'failed' not in result: if 'failed' not in result:
@ -663,10 +666,13 @@ class TaskExecutor:
if self._task.register: if self._task.register:
variables[self._task.register] = wrap_var(result) variables[self._task.register] = wrap_var(result)
if 'ansible_facts' in result and self._task.action not in ('set_fact', 'include_vars'): if 'ansible_facts' in result:
variables.update(namespace_facts(result['ansible_facts'])) if self._task.action in ('set_fact', 'include_vars'):
if C.INJECT_FACTS_AS_VARS: variables.update(result['ansible_facts'])
variables.update(clean_facts(result['ansible_facts'])) else:
variables.update(namespace_facts(result['ansible_facts']))
if C.INJECT_FACTS_AS_VARS:
variables.update(clean_facts(result['ansible_facts']))
# save the notification target in the result, if it was specified, as # save the notification target in the result, if it was specified, as
# this task may be running in a loop in which case the notification # this task may be running in a loop in which case the notification

@ -0,0 +1,19 @@
- name: test set_fact incremental https://github.com/ansible/ansible/issues/38271
hosts: testhost
gather_facts: no
tasks:
- name: Generate inline loop for set_fact
set_fact:
dig_list: "{{ dig_list + [ item ] }}"
loop:
- two
- three
- four
vars:
dig_list:
- one
- name: verify cumulative set fact worked
assert:
that:
- dig_list == ['one', 'two', 'three', 'four']

@ -0,0 +1,7 @@
- name: Test no warnings ref "http://github.com/ansible/ansible/issues/37535"
hosts: testhost
gather_facts: false
tasks:
- name: set ssh jump host args
set_fact:
ansible_ssh_common_args: "-o ProxyCommand='ssh -W %h:%p -q root@localhost'"

@ -5,9 +5,15 @@ set -eux
MYTMPDIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir') MYTMPDIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir')
trap 'rm -rf "${MYTMPDIR}"' EXIT trap 'rm -rf "${MYTMPDIR}"' EXIT
ANSIBLE_CACHE_PLUGIN=jsonfile ANSIBLE_CACHE_PLUGIN_CONNECTION="${MYTMPDIR}" ansible-playbook -i ../../inventory "$@" set_fact_cached_1.yml # ensure we can incrementally set fact via loopi, injection or not
ANSIBLE_INJECT_FACT_VARS=0 ansible-playbook -i ../../inventory incremental.yml
ANSIBLE_INJECT_FACT_VARS=1 ansible-playbook -i ../../inventory incremental.yml
ANSIBLE_CACHE_PLUGIN=jsonfile ANSIBLE_CACHE_PLUGIN_CONNECTION="${MYTMPDIR}" ansible-playbook -i ../../inventory "$@" set_fact_cached_2.yml # ensure we dont have spurious warnings do to clean_facts
ansible-playbook -i ../../inventory nowarn_clean_facts.yml | grep '[WARNING]: Removed restricted key from module data: ansible_ssh_common_args' && exit 1
ANSIBLE_CACHE_PLUGIN=jsonfile ANSIBLE_CACHE_PLUGIN_CONNECTION="${MYTMPDIR}" ansible-playbook -i ../../inventory --flush-cache "$@" set_fact_no_cache.yml
# test cached feature
export ANSIBLE_CACHE_PLUGIN=jsonfile ANSIBLE_CACHE_PLUGIN_CONNECTION="${MYTMPDIR}"
ansible-playbook -i ../../inventory "$@" set_fact_cached_1.yml
ansible-playbook -i ../../inventory "$@" set_fact_cached_2.yml
ansible-playbook -i ../../inventory --flush-cache "$@" set_fact_no_cache.yml

Loading…
Cancel
Save