Fix missing delegate display (#74370)

* dont rely on vars, task already gives us info
* ensure we always display delegation in host label
* also added parens with ansible_host to show  target host vs resolved host
* delegating to self is not delegating
* delegated vars restoration for backwards compat
* tests need mock task with delegate_to
pull/74495/head
Brian Coca 4 years ago committed by GitHub
parent 26214788ee
commit 3cff54d69b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- callbacks, restore displaying delegation to host as host label.

@ -728,6 +728,11 @@ class TaskExecutor:
for k in plugin_vars: for k in plugin_vars:
result["_ansible_delegated_vars"][k] = cvars.get(k) result["_ansible_delegated_vars"][k] = cvars.get(k)
# note: here for callbacks that rely on this info to display delegation
for requireshed in ('ansible_host', 'ansible_port', 'ansible_user', 'ansible_connection'):
if requireshed not in result["_ansible_delegated_vars"] and requireshed in cvars:
result["_ansible_delegated_vars"][requireshed] = cvars.get(requireshed)
# and return # and return
display.debug("attempt loop complete, returning result") display.debug("attempt loop complete, returning result")
return result return result

@ -21,9 +21,7 @@ __metaclass__ = type
import difflib import difflib
import json import json
import os
import sys import sys
import warnings
from copy import deepcopy from copy import deepcopy
@ -104,11 +102,15 @@ class CallbackBase(AnsiblePlugin):
"""Return label for the hostname (& delegated hostname) of a task """Return label for the hostname (& delegated hostname) of a task
result. result.
""" """
hostname = result._host.get_name() label = "%s" % result._host.get_name()
delegated_vars = result._result.get('_ansible_delegated_vars', None) if result._task.delegate_to and result._task.delegate_to != result._host.get_name():
if delegated_vars: # show delegated host
return "%s -> %s" % (hostname, delegated_vars['ansible_host']) label += " -> %s" % result._task.delegate_to
return "%s" % (hostname,) # in case we have 'extra resolution'
ahost = result._result.get('_ansible_delegated_vars', {}).get('ansible_host', result._task.delegate_to)
if result._task.delegate_to != ahost:
label += "(%s)" % ahost
return label
def _run_is_verbose(self, result, verbosity=0): def _run_is_verbose(self, result, verbosity=0):
return ((self._display.verbosity > verbosity or result._result.get('_ansible_verbose_always', False) is True) return ((self._display.verbosity > verbosity or result._result.get('_ansible_verbose_always', False) is True)

@ -32,6 +32,10 @@ from ansible.inventory.host import Host
from ansible.plugins.callback import CallbackBase from ansible.plugins.callback import CallbackBase
mock_task = MagicMock()
mock_task.delegate_to = None
class TestCallback(unittest.TestCase): class TestCallback(unittest.TestCase):
# FIXME: This doesn't really test anything... # FIXME: This doesn't really test anything...
def test_init(self): def test_init(self):
@ -50,13 +54,15 @@ class TestCallback(unittest.TestCase):
self.assertIs(cb._display, display_mock) self.assertIs(cb._display, display_mock)
def test_host_label(self): def test_host_label(self):
result = TaskResult(host=Host('host1'), task=None, return_data={}) result = TaskResult(host=Host('host1'), task=mock_task, return_data={})
self.assertEquals(CallbackBase.host_label(result), 'host1') self.assertEquals(CallbackBase.host_label(result), 'host1')
def test_host_label_delegated(self): def test_host_label_delegated(self):
mock_task.delegate_to = 'host2'
result = TaskResult( result = TaskResult(
host=Host('host1'), host=Host('host1'),
task=None, task=mock_task,
return_data={'_ansible_delegated_vars': {'ansible_host': 'host2'}}, return_data={'_ansible_delegated_vars': {'ansible_host': 'host2'}},
) )
self.assertEquals(CallbackBase.host_label(result), 'host1 -> host2') self.assertEquals(CallbackBase.host_label(result), 'host1 -> host2')

Loading…
Cancel
Save