From c012acd24c3f24acd3ba14f31b306f5bf6c51090 Mon Sep 17 00:00:00 2001 From: LegenJCdary Date: Mon, 30 Oct 2023 15:19:00 +0100 Subject: [PATCH] Enable --runner-plugins flag - this allows adding more callback plugins Define REQUIRED_CALLBACK_PLUGINS and SUPPORTED_CALLBACK_PLUGINS in global vars. Currently both are set to log_plays_adjusted and sqlite_deployer. REQUIRED_CALLBACK_PLUGINS are always enabled. SUPPORTED_CALLBACK_PLUGINS need to be present under source/plugins and contain class CallbackModule(child of CallbackBase). --- source/modules/globalvars.py | 2 ++ source/modules/misc/arguments.py | 24 ++++++++++++++++++++++++ source/modules/runners/run.py | 6 ++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/source/modules/globalvars.py b/source/modules/globalvars.py index d48e7250..b768cf82 100644 --- a/source/modules/globalvars.py +++ b/source/modules/globalvars.py @@ -13,3 +13,5 @@ ANSIBLE_DEFAULT_CALLBACK_PLUGIN_PATH = '~/.ansible/plugins/callback:/usr/share/ansible/plugins/' \ 'callback' SUPPORTED_STDOUT_CALLBACK_PLUGINS = ["json", "null", "yaml"] +REQUIRED_CALLBACK_PLUGINS = ["log_plays_adjusted", "sqlite_deployer"] +SUPPORTED_CALLBACK_PLUGINS = ["log_plays_adjusted", "sqlite_deployer"] diff --git a/source/modules/misc/arguments.py b/source/modules/misc/arguments.py index b6a788f5..e45644b1 100644 --- a/source/modules/misc/arguments.py +++ b/source/modules/misc/arguments.py @@ -50,6 +50,8 @@ def create_parser() -> ArgumentParser: help='Disable coloring of console messages.') parser.add_argument("--no-lock", default=False, action="store_true", help='Do not lock the infrastructure') + parser.add_argument("--runner-plugins", nargs=1, default=[None], metavar='TASK_NAME', + help='Provide comma-separated list of plugins to enable.') parser.add_argument("--runner-raw-file", default=False, action="store_true", help='Print original messages to main log file.') parser.add_argument("--runner-raw-output", default=False, action="store_true", @@ -131,6 +133,8 @@ def validate_rest_arguments(self, arguments: Namespace, print_end: str, print_fa options["lock"] = not arguments.no_lock options["no_color"] = arguments.no_color options["raw_output"] = arguments.runner_raw_output + options["runner_plugins"] = self.validate_ansible_callback( + arguments.runner_plugins[0], print_fail, print_end) options["runner_raw_file"] = arguments.runner_raw_file options["runner_stdout"] = self.validate_ansible_stdout_callback(arguments.runner_stdout, print_fail, print_end) @@ -156,3 +160,23 @@ def validate_ansible_stdout_callback(stdout_plugin: str, print_fail: str, print_ sys.exit(57) except AttributeError: return None + + @staticmethod + def validate_ansible_callback(stdout_plugin: str, print_fail: str, print_end: str + ) -> Optional[list]: + """Validate whether plugin (specified via --runner-plugin option) is supported""" + if stdout_plugin: + try: + plugins = stdout_plugin.split(",") + if all(plugin in globalvars.SUPPORTED_CALLBACK_PLUGINS for plugin in plugins): + return plugins + + print(f"{print_fail}[CRITICAL]: Unsupported runner callback plugin!" + f"{print_end}") + sys.exit(57) + except Exception: + print(f"{print_fail}[CRITICAL]: Invalid format of runner callback plugin!" + f"{print_end}") + sys.exit(57) + else: + return [] diff --git a/source/modules/runners/run.py b/source/modules/runners/run.py index aaf86e91..52ae9340 100644 --- a/source/modules/runners/run.py +++ b/source/modules/runners/run.py @@ -5,7 +5,8 @@ import subprocess from logging import Logger -from ansible_deployer.modules.globalvars import ANSIBLE_DEFAULT_CALLBACK_PLUGIN_PATH +from ansible_deployer.modules.globalvars import ANSIBLE_DEFAULT_CALLBACK_PLUGIN_PATH,\ + REQUIRED_CALLBACK_PLUGINS from ansible_deployer.modules.outputs.formatting import Formatters from ansible_deployer.modules.outputs.loggers import RunLogger @@ -229,10 +230,11 @@ def construct_command(self, playitem: str, inventory: str, config: dict, options def construct_env(self, options: dict, callback_settings: dict) -> dict: """Create final ansible environment from available variables""" + ansible_callbacks = list(set(options["runner_plugins"] + REQUIRED_CALLBACK_PLUGINS)) ansible_stdout_callback = options["runner_stdout"] if options["runner_stdout"]\ else callback_settings["def_stdout_plugin"] return dict( - os.environ, ANSIBLE_CALLBACKS_ENABLED="log_plays_adjusted,sqlite_deployer", + os.environ, ANSIBLE_CALLBACKS_ENABLED=",".join(ansible_callbacks), ANSIBLE_CALLBACK_PLUGINS=self.append_to_ansible_callbacks_path(), ANSIBLE_LOAD_CALLBACK_PLUGINS="1", ANSIBLE_NOCOWS="1", LOG_PLAYS_PATH=self.log_path, ANSIBLE_STDOUT_CALLBACK=ansible_stdout_callback, SQLITE_PATH=self.db_path,