From 9d8721dbdee25aef0c87daab4c6d4bf8b749ce1d Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 19 Jun 2017 17:03:28 -0400 Subject: [PATCH] properly track delegated host in loops ansible_host can be pulled from inventory and not match inventory_hostname, this can "loose" vars to a new host named by ansible_host vs the delegated host fixes #25770 --- lib/ansible/executor/task_executor.py | 2 +- lib/ansible/plugins/strategy/__init__.py | 2 +- lib/ansible/vars/manager.py | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py index ecdac5ee401..85e334b9a6a 100644 --- a/lib/ansible/executor/task_executor.py +++ b/lib/ansible/executor/task_executor.py @@ -628,7 +628,7 @@ class TaskExecutor: delegated_vars = variables.get('ansible_delegated_vars', dict()).get(self._task.delegate_to, dict()).copy() if len(delegated_vars) > 0: result["_ansible_delegated_vars"] = dict() - for k in ('ansible_host', ): + for k in ('ansible_delegated_host', 'ansible_host' ): result["_ansible_delegated_vars"][k] = delegated_vars.get(k) # and return diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py index 2b10f87b7a1..8cb918f50a2 100644 --- a/lib/ansible/plugins/strategy/__init__.py +++ b/lib/ansible/plugins/strategy/__init__.py @@ -245,7 +245,7 @@ class StrategyBase: return host_list def get_delegated_hosts(self, result, task): - host_name = result.get('_ansible_delegated_vars', {}).get('ansible_host', None) + host_name = result.get('_ansible_delegated_vars', {}).get('ansible_delegated_host', None) if host_name is not None: actual_host = self._inventory.get_host(host_name) if actual_host is None: diff --git a/lib/ansible/vars/manager.py b/lib/ansible/vars/manager.py index 6747dff3775..7dd4e205b09 100644 --- a/lib/ansible/vars/manager.py +++ b/lib/ansible/vars/manager.py @@ -524,7 +524,8 @@ class VariableManager: new_port = 5986 new_delegated_host_vars = dict( - ansible_host=delegated_host_name, + ansible_delegated_host=delegated_host_name, + ansible_host=delegated_host_name, # not redundant as other sources can change ansible_host ansible_port=new_port, ansible_user=C.DEFAULT_REMOTE_USER, ansible_connection=C.DEFAULT_TRANSPORT,