Ensure the correct connection name is shown in results (#83354)

pull/83489/head
Brian Coca 5 months ago committed by GitHub
parent f7dee8aaf8
commit 339452c105
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,2 @@
bugfixes:
- Callbacks now correctly get the resolved connection plugin name as the connection used.

@ -21,6 +21,7 @@ from ansible.module_utils.parsing.convert_bool import boolean
from ansible.module_utils.six import binary_type from ansible.module_utils.six import binary_type
from ansible.module_utils.common.text.converters import to_text, to_native from ansible.module_utils.common.text.converters import to_text, to_native
from ansible.module_utils.connection import write_to_stream from ansible.module_utils.connection import write_to_stream
from ansible.module_utils.six import string_types
from ansible.playbook.conditional import Conditional from ansible.playbook.conditional import Conditional
from ansible.playbook.task import Task from ansible.playbook.task import Task
from ansible.plugins import get_plugin_class from ansible.plugins import get_plugin_class
@ -372,12 +373,17 @@ class TaskExecutor:
'msg': 'Failed to template loop_control.label: %s' % to_text(e) 'msg': 'Failed to template loop_control.label: %s' % to_text(e)
}) })
# if plugin is loaded, get resolved name, otherwise leave original task connection
if self._connection and not isinstance(self._connection, string_types):
task_fields['connection'] = getattr(self._connection, 'ansible_name')
tr = TaskResult( tr = TaskResult(
self._host.name, self._host.name,
self._task._uuid, self._task._uuid,
res, res,
task_fields=task_fields, task_fields=task_fields,
) )
if tr.is_failed() or tr.is_unreachable(): if tr.is_failed() or tr.is_unreachable():
self._final_q.send_callback('v2_runner_item_on_failed', tr) self._final_q.send_callback('v2_runner_item_on_failed', tr)
elif tr.is_skipped(): elif tr.is_skipped():

@ -0,0 +1,40 @@
# Copyright: Contributors to the Ansible project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import annotations
DOCUMENTATION = '''
name: track_connections
short_description: Track connection plugins used for hosts
description:
- Track connection plugins used for hosts
type: aggregate
'''
import json
from collections import defaultdict
from ansible.plugins.callback import CallbackBase
class CallbackModule(CallbackBase):
CALLBACK_VERSION = 2.0
CALLBACK_TYPE = 'aggregate'
CALLBACK_NAME = 'track_connections'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._conntrack = defaultdict(lambda : defaultdict(int))
def _track(self, result, *args, **kwargs):
host = result._host.get_name()
task = result._task
self._conntrack[host][task.connection] += 1
v2_runner_on_ok = v2_runner_on_failed = _track
v2_runner_on_async_poll = v2_runner_on_async_ok = v2_runner_on_async_failed = _track
v2_runner_item_on_ok = v2_runner_item_on_failed = _track
def v2_playbook_on_stats(self, stats):
self._display.display(json.dumps(self._conntrack, indent=4))

@ -4,10 +4,17 @@ set -eux
# we are looking to verify the callback for v2_retry_runner gets a correct task name, include # we are looking to verify the callback for v2_retry_runner gets a correct task name, include
# if the value needs templating based on results of previous tasks # if the value needs templating based on results of previous tasks
OUTFILE="callback_retry_task_name.out" OUTFILE="callback_output_copy.out"
trap 'rm -rf "${OUTFILE}"' EXIT trap 'rm -rf "${OUTFILE}"' EXIT
# test task retry name
EXPECTED_REGEX="^.*TASK.*18236 callback task template fix OUTPUT 2" EXPECTED_REGEX="^.*TASK.*18236 callback task template fix OUTPUT 2"
ansible-playbook "$@" -i ../../inventory test.yml | tee "${OUTFILE}" ansible-playbook "$@" -i ../../inventory task_name.yml | tee "${OUTFILE}"
echo "Grepping for ${EXPECTED_REGEX} in stdout." echo "Grepping for ${EXPECTED_REGEX} in stdout."
grep -e "${EXPECTED_REGEX}" "${OUTFILE}" grep -e "${EXPECTED_REGEX}" "${OUTFILE}"
# test connection tracking
EXPECTED_CONNECTION='{"testhost":{"ssh":4}}'
OUTPUT_TAIL=$(tail -n5 ${OUTFILE} | tr -d '[:space:]')
[ "${EXPECTED_CONNECTION}" == "${OUTPUT_TAIL}" ]
echo $?
Loading…
Cancel
Save