diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py index 5f9cccae4c..1417bc9d2c 100644 --- a/lib/ansible/executor/task_executor.py +++ b/lib/ansible/executor/task_executor.py @@ -367,7 +367,7 @@ class TaskExecutor: self._task.args = variable_params # get the connection and the handler for this execution - if not self._connection or not getattr(self._connection, 'connected', False): + if not self._connection or not getattr(self._connection, 'connected', False) or self._play_context.remote_addr != self._connection._play_context.remote_addr: self._connection = self._get_connection(variables=variables, templar=templar) self._connection.set_host_overrides(host=self._host) else: diff --git a/lib/ansible/playbook/play_context.py b/lib/ansible/playbook/play_context.py index 6b19f4c172..409f9661b8 100644 --- a/lib/ansible/playbook/play_context.py +++ b/lib/ansible/playbook/play_context.py @@ -366,12 +366,17 @@ class PlayContext(Base): else: delegated_vars = dict() + attrs_considered = [] for (attr, variable_names) in iteritems(MAGIC_VARIABLE_MAPPING): for variable_name in variable_names: + if attr in attrs_considered: + continue if isinstance(delegated_vars, dict) and variable_name in delegated_vars: setattr(new_info, attr, delegated_vars[variable_name]) + attrs_considered.append(attr) elif variable_name in variables: setattr(new_info, attr, variables[variable_name]) + attrs_considered.append(attr) # make sure we get port defaults if needed if new_info.port is None and C.DEFAULT_REMOTE_PORT is not None: