Make sure set_fact variables go into VARS_CACHE

Fixes #8758
This commit is contained in:
James Cammarata 2014-09-03 09:39:50 -05:00
parent 893f15b30b
commit 14b8e2cf01
2 changed files with 21 additions and 3 deletions

View file

@ -487,11 +487,20 @@ class PlayBook(object):
def _register_play_vars(host, result): def _register_play_vars(host, result):
# when 'register' is used, persist the result in the vars cache # when 'register' is used, persist the result in the vars cache
# rather than the setup cache - vars should be transient between playbook executions # rather than the setup cache - vars should be transient between
# playbook executions
if 'stdout' in result and 'stdout_lines' not in result: if 'stdout' in result and 'stdout_lines' not in result:
result['stdout_lines'] = result['stdout'].splitlines() result['stdout_lines'] = result['stdout'].splitlines()
utils.update_hash(self.VARS_CACHE, host, {task.register: result}) utils.update_hash(self.VARS_CACHE, host, {task.register: result})
def _save_play_facts(host, facts):
# saves play facts in SETUP_CACHE, unless the module executed was
# set_fact, in which case we add them to the VARS_CACHE
if task.module_name == 'set_fact':
utils.update_hash(self.VARS_CACHE, host, facts)
else:
utils.update_hash(self.SETUP_CACHE, host, facts)
# add facts to the global setup cache # add facts to the global setup cache
for host, result in contacted.iteritems(): for host, result in contacted.iteritems():
if 'results' in result: if 'results' in result:
@ -500,11 +509,13 @@ class PlayBook(object):
for res in result['results']: for res in result['results']:
if type(res) == dict: if type(res) == dict:
facts = res.get('ansible_facts', {}) facts = res.get('ansible_facts', {})
utils.update_hash(self.SETUP_CACHE, host, facts) _save_play_facts(host, facts)
else: else:
# when facts are returned, persist them in the setup cache # when facts are returned, persist them in the setup cache
facts = result.get('ansible_facts', {}) facts = result.get('ansible_facts', {})
utils.update_hash(self.SETUP_CACHE, host, facts) _save_play_facts(host, facts)
# if requested, save the result into the registered variable name
if task.register: if task.register:
_register_play_vars(host, result) _register_play_vars(host, result)

View file

@ -9,13 +9,20 @@
roles: roles:
- { role: test_var_precedence, param_var: "param_var" } - { role: test_var_precedence, param_var: "param_var" }
tasks: tasks:
- name: register a result
command: echo 'BAD!'
register: registered_var
- name: use set_fact to override the registered_var
set_fact: registered_var="this is from set_fact"
- debug: var=extra_var - debug: var=extra_var
- debug: var=vars_var - debug: var=vars_var
- debug: var=vars_files_var - debug: var=vars_files_var
- debug: var=vars_files_var_role - debug: var=vars_files_var_role
- debug: var=registered_var
- assert: - assert:
that: that:
- 'extra_var == "extra_var"' - 'extra_var == "extra_var"'
- 'vars_var == "vars_var"' - 'vars_var == "vars_var"'
- 'vars_files_var == "vars_files_var"' - 'vars_files_var == "vars_files_var"'
- 'vars_files_var_role == "vars_files_var_role3"' - 'vars_files_var_role == "vars_files_var_role3"'
- 'registered_var == "this is from set_fact"'