Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing delegate display #74370

Merged
merged 6 commits into from Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/restore_delegate_label.yml
@@ -0,0 +1,2 @@
bugfixes:
- callbacks, restore displaying delegation to host as host label.
5 changes: 5 additions & 0 deletions lib/ansible/executor/task_executor.py
Expand Up @@ -728,6 +728,11 @@ def _evaluate_failed_when_result(result):
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'):
bcoca marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
16 changes: 9 additions & 7 deletions lib/ansible/plugins/callback/__init__.py
Expand Up @@ -21,9 +21,7 @@

import difflib
import json
import os
import sys
import warnings

from copy import deepcopy

Expand Down Expand Up @@ -104,11 +102,15 @@ def host_label(result):
"""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)
Expand Down
10 changes: 8 additions & 2 deletions test/units/plugins/callback/test_callback.py
Expand Up @@ -32,6 +32,10 @@
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):
Expand All @@ -50,13 +54,15 @@ def test_display_verbose(self):
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')
Expand Down