From 14b8e2cf014c04b83b08f6bd5964e7d1d4010076 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Wed, 3 Sep 2014 09:39:50 -0500 Subject: [PATCH] Make sure set_fact variables go into VARS_CACHE Fixes #8758 --- lib/ansible/playbook/__init__.py | 17 ++++++++++++++--- test/integration/test_var_precedence.yml | 7 +++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/ansible/playbook/__init__.py b/lib/ansible/playbook/__init__.py index e2e2aea3e5b..4acb15a651f 100644 --- a/lib/ansible/playbook/__init__.py +++ b/lib/ansible/playbook/__init__.py @@ -487,11 +487,20 @@ class PlayBook(object): def _register_play_vars(host, result): # 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: result['stdout_lines'] = result['stdout'].splitlines() 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 for host, result in contacted.iteritems(): if 'results' in result: @@ -500,11 +509,13 @@ class PlayBook(object): for res in result['results']: if type(res) == dict: facts = res.get('ansible_facts', {}) - utils.update_hash(self.SETUP_CACHE, host, facts) + _save_play_facts(host, facts) else: # when facts are returned, persist them in the setup cache 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: _register_play_vars(host, result) diff --git a/test/integration/test_var_precedence.yml b/test/integration/test_var_precedence.yml index 301f6cdb311..722993e02fa 100644 --- a/test/integration/test_var_precedence.yml +++ b/test/integration/test_var_precedence.yml @@ -9,13 +9,20 @@ roles: - { role: test_var_precedence, param_var: "param_var" } 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=vars_var - debug: var=vars_files_var - debug: var=vars_files_var_role + - debug: var=registered_var - assert: that: - 'extra_var == "extra_var"' - 'vars_var == "vars_var"' - 'vars_files_var == "vars_files_var"' - 'vars_files_var_role == "vars_files_var_role3"' + - 'registered_var == "this is from set_fact"'