diff options
author | Brian Coca <brian.coca+git@gmail.com> | 2017-02-27 11:00:48 -0500 |
---|---|---|
committer | Brian Coca <bcoca@users.noreply.github.com> | 2017-03-15 16:02:36 -0400 |
commit | de331a94886cfc33b5ab081c6332ff22989a8ba5 (patch) | |
tree | 654629f4719323e92a8fe7fadee0c3f53eb91f48 | |
parent | 36b462ba857361ba310c7967a721c025c13d2225 (diff) | |
download | ansible-de331a94886cfc33b5ab081c6332ff22989a8ba5.tar.gz |
clarify facts assignment for several corner cases
run_once/delegate_facts:
now delegate_facts > run_once, previously run_once always published facts to all hosts in play
include_vars/delegate_to:
now include_vars allows to delegate to a specific host
also fix task_vars exception in delegate_facts/loop as var was removed
fixes #15365
(cherry picked from commit 519f5db7ccb2f0114485521d432fb008c5b2dfce)
-rw-r--r-- | lib/ansible/plugins/strategy/__init__.py | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py index 7588716b88..2d0758c331 100644 --- a/lib/ansible/plugins/strategy/__init__.py +++ b/lib/ansible/plugins/strategy/__init__.py @@ -236,6 +236,15 @@ class StrategyBase: host_list = [task_host] return host_list + def get_delegated_hosts(self, result, loop_var, task): + + host_name = task.delegate_to + actual_host = self._inventory.get_host(host_name) + if actual_host is None: + actual_host = Host(name=host_name) + + return [actual_host] + def _process_pending_results(self, iterator, one_pass=False, max_passes=None): ''' Reads results off the final queue and takes appropriate action @@ -483,27 +492,25 @@ class StrategyBase: if 'ansible_facts' in result_item: - # if delegated fact and we are delegating facts, we need to change target host for them - if original_task.delegate_to is not None and original_task.delegate_facts: - item = result_item.get(loop_var, None) - if item is not None: - task_vars[loop_var] = item - host_name = templar.template(original_task.delegate_to) - actual_host = self._inventory.get_host(host_name) - if actual_host is None: - actual_host = Host(name=host_name) - else: - actual_host = original_host - - host_list = self.get_task_hosts(iterator, actual_host, original_task) if original_task.action == 'include_vars': + if original_task.delegate_to is not None: + host_list = self.get_delegated_hosts(result_item, loop_var, original_task) + else: + host_list = self.get_task_hosts(iterator, original_host, original_task) + for (var_name, var_value) in iteritems(result_item['ansible_facts']): # find the host we're actually refering too here, which may # be a host that is not really in inventory at all for target_host in host_list: self._variable_manager.set_host_variable(target_host, var_name, var_value) else: + # if delegated fact and we are delegating facts, we need to change target host for them + if original_task.delegate_to is not None and original_task.delegate_facts: + host_list = self.get_delegated_hosts(result_item, loop_var, original_task) + else: + host_list = self.get_task_hosts(iterator, original_host, original_task) + 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()) |