diff --git a/lib/ansible/playbook/play_context.py b/lib/ansible/playbook/play_context.py index 8edaa12a033..b2b7e444975 100644 --- a/lib/ansible/playbook/play_context.py +++ b/lib/ansible/playbook/play_context.py @@ -334,6 +334,8 @@ class PlayContext(Base): display.debug("no remote address found for delegated host %s\nusing its name, so success depends on DNS resolution" % delegated_host_name) delegated_vars['ansible_host'] = delegated_host_name + # reset the port back to the default if none was specified, to prevent + # the delegated host from inheriting the original host's setting for port_var in MAGIC_VARIABLE_MAPPING.get('port'): if port_var in delegated_vars: break @@ -342,6 +344,13 @@ class PlayContext(Base): delegated_vars['ansible_port'] = 5986 else: delegated_vars['ansible_port'] = C.DEFAULT_REMOTE_PORT + + # and likewise for the remote user + for user_var in MAGIC_VARIABLE_MAPPING.get('remote_user'): + if user_var in delegated_vars: + break + else: + delegated_vars['ansible_user'] = None else: delegated_vars = dict() diff --git a/test/units/playbook/test_play_context.py b/test/units/playbook/test_play_context.py index 0595966f1bd..5434ef30004 100644 --- a/test/units/playbook/test_play_context.py +++ b/test/units/playbook/test_play_context.py @@ -91,6 +91,7 @@ class TestPlayContext(unittest.TestCase): mock_task.become_user = 'mocktaskroot' mock_task.become_pass = 'mocktaskpass' mock_task._local_action = False + mock_task.delegate_to = None all_vars = dict( ansible_connection = 'mock_inventory',