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

ansible: add module_defaults callback, remove deprecated gather_subset in config #873

Merged
merged 2 commits into from
Feb 13, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public static AnsibleCallbacks process(AnsibleContext ctx, AnsibleConfig config)
private static final String[] LOG_FILTERING_CALLBACKS = new String[]{
"concord_protectdata.py"
};
private static final String[] MODULE_DEFAULTS_CALLBACKS = new String[]{
"concord_default_module_args.py"
};

private final boolean debug;
private final Path workDir;
Expand All @@ -77,6 +80,7 @@ public static AnsibleCallbacks process(AnsibleContext ctx, AnsibleConfig config)
private boolean eventsEnabled = false;
private boolean statsEnabled = false;
private boolean outVarsEnabled = false;
private boolean moduleDefaultsEnabled = false;

private Path eventsFile;
private EventSender eventSender;
Expand All @@ -95,7 +99,8 @@ public AnsibleCallbacks parse(Map<String, Object> args) {

this.eventsEnabled = MapUtils.getBoolean(args, TaskParams.ENABLE_EVENTS, true);
this.statsEnabled = MapUtils.getBoolean(args, TaskParams.ENABLE_STATS, true);
this.outVarsEnabled= MapUtils.getBoolean(args, TaskParams.ENABLE_OUT_VARS, true);
this.outVarsEnabled = MapUtils.getBoolean(args, TaskParams.ENABLE_OUT_VARS, true);
this.moduleDefaultsEnabled = MapUtils.getBoolean(args, TaskParams.ENABLE_MODULE_DEFAULTS, true);

return this;
}
Expand Down Expand Up @@ -125,6 +130,10 @@ public AnsibleCallbacks write() {
if (outVarsEnabled) {
Resources.copy(CALLBACK_LOCATION, OUTVARS_CALLBACKS, getDir());
}

if (moduleDefaultsEnabled) {
Resources.copy(CALLBACK_LOCATION, MODULE_DEFAULTS_CALLBACKS, getDir());
}
} catch (IOException e) {
log.error("Error while adding Concord callback plugins: {}", e.getMessage(), e);
throw new RuntimeException("Error while adding Concord callback plugins: " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ private Path getConfigPath() {
private static Map<String, Object> makeDefaults() {
Map<String, Object> m = new HashMap<>();

// disable puppet / chef fact gathering, significant speed/performance increase - usually unneeded
// may eventually need !hardware for AIX/HPUX or set at runtime, Ansible 2.4 fixes many broken facts
m.put("gather_subset", "!facter,!ohai");

// disable ssl host key checking by default
m.put("host_key_checking", false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public enum TaskParams implements HasKey {

ENABLE_OUT_VARS("enableOutsVars"),

ENABLE_MODULE_DEFAULTS("enableModuleDefaults"),

EXIT_CODE_KEY("exitCode"),

EXTRA_ENV_KEY("extraEnv"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from ansible.plugins.callback import CallbackBase
from ansible.playbook.task import Task
from ansible.inventory.host import Host


class CallbackModule(CallbackBase):
def v2_runner_on_start(self, host: Host, task: Task):
if task.resolved_action == 'ansible.builtin.gather_facts' or task.resolved_action == 'gather_facts':
# disable puppet / chef fact gathering, significant speed/performance increase - usually unneeded
# may eventually need !hardware for AIX/HPUX or set at runtime, Ansible 2.4 fixes many broken facts
module_defaults = self._get_defaults(task)

if ((task.resolved_action not in module_defaults) or
module_defaults[task.resolved_action]['gather_subset'] is None):

# no module_defaults are set for this module
# now make sure there are no task args
if 'gather_subset' not in task.args or task.args['gather_subset'] is None:
task.args['gather_subset'] = ['!facter', '!ohai']

def _get_defaults(self, task: Task):
for mod_def in task.module_defaults:
if task.resolved_action in mod_def and mod_def[task.resolved_action] is not None:
return mod_def

return dict()
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
callback_plugins = _callbacks
host_key_checking = false
retry_files_enabled = true
gather_subset = !facter,!ohai
remote_tmp = /tmp/${USER}/ansible
timeout = 120
lookup_plugins = _lookups
Expand Down
Loading