Add set_fact_persistent action and module. (#26153)

* Add 'cacheable' param to  set_fact action and module.

Used just like set_fact, except facts set with cacheable: true
will be stored in the fact cache if fact caching is enabled.

set_fact normally only sets facts in the non_persistent_fact_cache, so they
are lost between invocations.

* update set_facts docs

* use 'ansible_facts_cacheable' in module/actions result

* pop fact cacheable related items out of args/results

We dont want to use 'ansible_facts_cacheable' result item
or 'cacheable' arg as actual facts, so pop them out of the
dicts.
This commit is contained in:
Adrian Likins 2017-08-02 15:57:58 -04:00 committed by GitHub
parent 3476b005b9
commit 6fbd0a8bb5
8 changed files with 125 additions and 3 deletions

View file

@ -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:

View file

@ -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

View file

@ -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']:

View file

@ -0,0 +1 @@
posix/ci/group3

View file

@ -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

View file

@ -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'

View file

@ -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

View file

@ -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