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
This commit is contained in:
Brian Coca 2021-04-28 13:24:38 -04:00 committed by GitHub
parent 26214788ee
commit 3cff54d69b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 9 deletions

View file

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

View file

@ -728,6 +728,11 @@ class TaskExecutor:
for k in plugin_vars:
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
display.debug("attempt loop complete, returning result")
return result

View file

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

View file

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