diff --git a/lib/ansible/modules/utilities/logic/set_fact.py b/lib/ansible/modules/utilities/logic/set_fact.py index d8c3d83600..a2148e7ec3 100644 --- a/lib/ansible/modules/utilities/logic/set_fact.py +++ b/lib/ansible/modules/utilities/logic/set_fact.py @@ -33,11 +33,19 @@ options: using the C(args:) statement. required: true default: null + cacheable: + description: + - This boolean indicates if the facts set will also be added to the + fact cache, if fact caching is enabled. + required: false + default: false + version_added: "2.4" version_added: "1.2" notes: - "The `var=value` notation can only create strings or booleans. If you want to create lists/arrays or dictionary/hashes use `var: [val1, val2]`" - This module is also supported for Windows targets. + - Since 'cacheable' is now a module param, 'cacheable' is no longer a valid fact name as of 2.4. ''' EXAMPLES = ''' @@ -50,6 +58,12 @@ EXAMPLES = ''' other_fact: "{{ local_var * 2 }}" another_fact: "{{ some_registered_var.results | map(attribute='ansible_facts.some_fact') | list }}" +# Example setting facts so that they will be persisted in the fact cache +- set_fact: + one_fact: something + other_fact: "{{ local_var * 2 }}" + cacheable: true + # As of 1.8, Ansible will convert boolean strings ('true', 'false', 'yes', 'no') # to proper boolean values when using the key=value syntax, however it is still # recommended that booleans be set using the complex argument style: diff --git a/lib/ansible/plugins/action/set_fact.py b/lib/ansible/plugins/action/set_fact.py index 7c8f0346c9..65f3a8f322 100644 --- a/lib/ansible/plugins/action/set_fact.py +++ b/lib/ansible/plugins/action/set_fact.py @@ -35,6 +35,9 @@ class ActionModule(ActionBase): result = super(ActionModule, self).run(tmp, task_vars) facts = dict() + + cacheable = bool(self._task.args.pop('cacheable', False)) + if self._task.args: for (k, v) in iteritems(self._task.args): k = self._templar.template(k) @@ -51,4 +54,5 @@ class ActionModule(ActionBase): result['changed'] = False result['ansible_facts'] = facts + result['ansible_facts_cacheable'] = cacheable return result diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py index efb60c53af..e7b01f1a61 100644 --- a/lib/ansible/plugins/strategy/__init__.py +++ b/lib/ansible/plugins/strategy/__init__.py @@ -511,12 +511,14 @@ class StrategyBase: for target_host in host_list: self._variable_manager.set_host_variable(target_host, var_name, var_value) else: + cacheable = result_item.pop('ansible_facts_cacheable', True) for target_host in host_list: - if original_task.action == 'set_fact': - self._variable_manager.set_nonpersistent_facts(target_host, result_item['ansible_facts'].copy()) - else: + if cacheable: self._variable_manager.set_host_facts(target_host, result_item['ansible_facts'].copy()) + # If we are setting a fact, it should populate non_persistent_facts as well + self._variable_manager.set_nonpersistent_facts(target_host, result_item['ansible_facts'].copy()) + if 'ansible_stats' in result_item and 'data' in result_item['ansible_stats'] and result_item['ansible_stats']['data']: if 'per_host' not in result_item['ansible_stats'] or result_item['ansible_stats']['per_host']: diff --git a/test/integration/targets/set_fact/aliases b/test/integration/targets/set_fact/aliases new file mode 100644 index 0000000000..79d8b9285e --- /dev/null +++ b/test/integration/targets/set_fact/aliases @@ -0,0 +1 @@ +posix/ci/group3 diff --git a/test/integration/targets/set_fact/runme.sh b/test/integration/targets/set_fact/runme.sh new file mode 100755 index 0000000000..3597c77a22 --- /dev/null +++ b/test/integration/targets/set_fact/runme.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -eux + +MYTMPDIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir') +trap 'rm -rf "${MYTMPDIR}"' EXIT + +ANSIBLE_CACHE_PLUGIN=jsonfile ANSIBLE_CACHE_PLUGIN_CONNECTION="${MYTMPDIR}" ansible-playbook -i ../../inventory "$@" set_fact_cached_1.yml + +ANSIBLE_CACHE_PLUGIN=jsonfile ANSIBLE_CACHE_PLUGIN_CONNECTION="${MYTMPDIR}" ansible-playbook -i ../../inventory "$@" set_fact_cached_2.yml + +ANSIBLE_CACHE_PLUGIN=jsonfile ANSIBLE_CACHE_PLUGIN_CONNECTION="${MYTMPDIR}" ansible-playbook -i ../../inventory --flush-cache "$@" set_fact_no_cache.yml + diff --git a/test/integration/targets/set_fact/set_fact_cached_1.yml b/test/integration/targets/set_fact/set_fact_cached_1.yml new file mode 100644 index 0000000000..ffb03c8886 --- /dev/null +++ b/test/integration/targets/set_fact/set_fact_cached_1.yml @@ -0,0 +1,46 @@ +--- +- name: the first play + hosts: localhost + tasks: + - name: show foobar fact before + debug: + var: ansible_foobar + + - name: set a persistent fact foobar + set_fact: + ansible_foobar: 'blippy' + cacheable: true + + - name: show foobar fact after + debug: + var: ansible_foobar + + - name: assert ansible_foobar is correct value + assert: + that: + - ansible_foobar == 'blippy' + + - name: set a non persistent fact that will not be cached + set_fact: + ansible_foobar_not_cached: 'this_should_not_be_cached' + + - name: show ansible_foobar_not_cached fact after being set + debug: + var: ansible_foobar_not_cached + + - name: assert ansible_foobar_not_cached is correct value + assert: + that: + - ansible_foobar_not_cached == 'this_should_not_be_cached' + +- name: the second play + hosts: localhost + tasks: + - name: show foobar fact after second play + debug: + var: ansible_foobar + + - name: assert ansible_foobar is correct value + assert: + that: + - ansible_foobar == 'blippy' diff --git a/test/integration/targets/set_fact/set_fact_cached_2.yml b/test/integration/targets/set_fact/set_fact_cached_2.yml new file mode 100644 index 0000000000..8d7a9f613a --- /dev/null +++ b/test/integration/targets/set_fact/set_fact_cached_2.yml @@ -0,0 +1,21 @@ +--- +- name: A second playbook run with fact caching enabled + hosts: localhost + tasks: + - name: show ansible_foobar fact + debug: + var: ansible_foobar + + - name: assert ansible_foobar is correct value when read from cache + assert: + that: + - ansible_foobar == 'blippy' + + - name: show ansible_foobar_not_cached fact + debug: + var: ansible_foobar_not_cached + + - name: assert ansible_foobar_not_cached is not cached + assert: + that: + - ansible_foobar_not_cached is undefined diff --git a/test/integration/targets/set_fact/set_fact_no_cache.yml b/test/integration/targets/set_fact/set_fact_no_cache.yml new file mode 100644 index 0000000000..3dffb65f55 --- /dev/null +++ b/test/integration/targets/set_fact/set_fact_no_cache.yml @@ -0,0 +1,21 @@ +--- +- name: Running with fact caching enabled but with cache flushed + hosts: localhost + tasks: + - name: show ansible_foobar fact + debug: + var: ansible_foobar + + - name: assert ansible_foobar is correct value + assert: + that: + - ansible_foobar is undefined + + - name: show ansible_foobar_not_cached fact + debug: + var: ansible_foobar_not_cached + + - name: assert ansible_foobar_not_cached is not cached + assert: + that: + - ansible_foobar_not_cached is undefined