Skip to content

Commit

Permalink
Enable --runner-plugins flag - this allows adding more callback plugins
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
LegenJCdary committed Oct 1, 2024
1 parent faf35d3 commit c012acd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions source/modules/globalvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
24 changes: 24 additions & 0 deletions source/modules/misc/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand All @@ -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 []
6 changes: 4 additions & 2 deletions source/modules/runners/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c012acd

Please sign in to comment.