|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright (C) 2023, Red Hat, Inc. |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +from __future__ import absolute_import, division, print_function |
| 6 | + |
| 7 | +__metaclass__ = type |
| 8 | + |
| 9 | +DOCUMENTATION = """ |
| 10 | + author: Rich Megginson |
| 11 | + name: dump_packages |
| 12 | + type: aggregate |
| 13 | + short_description: dump arguments to package module |
| 14 | + description: |
| 15 | + - Dump arguments to package module to get list of packages. |
| 16 | + - Used in conjunction with CI testing to get the packages used |
| 17 | + - with all combinations of: distribution/version/role arguments |
| 18 | + - Used to generate lists of packages for ostree image builds. |
| 19 | + requirements: |
| 20 | + - None |
| 21 | +""" |
| 22 | + |
| 23 | +from ansible.plugins.callback import CallbackBase # noqa: E402 |
| 24 | + |
| 25 | + |
| 26 | +class CallbackModule(CallbackBase): |
| 27 | + """ |
| 28 | + Dump packages. |
| 29 | + """ |
| 30 | + |
| 31 | + CALLBACK_VERSION = 2.0 |
| 32 | + CALLBACK_TYPE = "aggregate" |
| 33 | + CALLBACK_NAME = "dump_packages" |
| 34 | + # needed for 2.9 compatibility |
| 35 | + CALLBACK_NEEDS_WHITELIST = False # wokeignore:rule=whitelist |
| 36 | + CALLBACK_NEEDS_ENABLED = False |
| 37 | + |
| 38 | + def __init__(self, *args, **kwargs): |
| 39 | + super(CallbackModule, self).__init__(*args, **kwargs) |
| 40 | + |
| 41 | + def v2_runner_on_ok(self, result): |
| 42 | + fields = result._task_fields |
| 43 | + if ( |
| 44 | + fields["action"] in ["package", "dnf", "yum"] |
| 45 | + and fields["args"].get("state") != "absent" |
| 46 | + ): |
| 47 | + packages = set() |
| 48 | + if "invocation" in result._result: |
| 49 | + results = [result._result] |
| 50 | + elif "results" in result._result and isinstance( |
| 51 | + result._result["results"], list |
| 52 | + ): |
| 53 | + results = result._result["results"] |
| 54 | + for item in results: |
| 55 | + pkgs = item["invocation"]["module_args"]["name"] |
| 56 | + if isinstance(pkgs, list): |
| 57 | + for ii in pkgs: |
| 58 | + packages.add(ii) |
| 59 | + else: |
| 60 | + packages.add(pkgs) |
| 61 | + # tell python black that this line is ok |
| 62 | + # fmt: off |
| 63 | + self._display.display("lsrpackages: " + " ".join(sorted(list(packages)))) |
| 64 | + # fmt: on |
0 commit comments