| 1 |
# Copyright (c) 2022, Alexei Znamensky <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 6 |
# Do not use this from other collections or standalone plugins/modules! |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import os |
| 11 |
import typing as t |
| 12 |
|
| 13 |
from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt |
| 14 |
|
| 15 |
if t.TYPE_CHECKING: |
| 16 |
from ansible.module_utils.basic import AnsibleModule |
| 17 |
|
| 18 |
|
| 19 |
_PUPPET_PATH_PREFIX = ["/opt/puppetlabs/bin"] |
| 20 |
|
| 21 |
|
| 22 |
def get_facter_dir() -> str: |
| 23 |
if os.getuid() == 0: |
| 24 |
return "/etc/facter/facts.d" |
| 25 |
else: |
| 26 |
return os.path.expanduser("~/.facter/facts.d") |
| 27 |
|
| 28 |
|
| 29 |
def _puppet_cmd(module: AnsibleModule) -> str | None: |
| 30 |
return module.get_bin_path("puppet", False, _PUPPET_PATH_PREFIX) |
| 31 |
|
| 32 |
|
| 33 |
# If the `timeout` CLI command feature is removed, |
| 34 |
# Then we could add this as a fixed param to `puppet_runner` |
| 35 |
def ensure_agent_enabled(module: AnsibleModule) -> None: |
| 36 |
runner = CmdRunner( |
| 37 |
module, |
| 38 |
command="puppet", |
| 39 |
path_prefix=_PUPPET_PATH_PREFIX, |
| 40 |
arg_formats=dict( |
| 41 |
_agent_disabled=cmd_runner_fmt.as_fixed(["config", "print", "agent_disabled_lockfile"]), |
| 42 |
), |
| 43 |
check_rc=False, |
| 44 |
) |
| 45 |
|
| 46 |
rc, stdout, stderr = runner("_agent_disabled").run() |
| 47 |
if os.path.exists(stdout.strip()): |
| 48 |
module.fail_json(msg="Puppet agent is administratively disabled.", disabled=True) |
| 49 |
elif rc != 0: |
| 50 |
module.fail_json(msg="Puppet agent state could not be determined.") |
| 51 |
|
| 52 |
|
| 53 |
def puppet_runner(module: AnsibleModule) -> CmdRunner: |
| 54 |
# Keeping backward compatibility, allow for running with the `timeout` CLI command. |
| 55 |
# If this can be replaced with ansible `timeout` parameter in playbook, |
| 56 |
# then this function could be removed. |
| 57 |
def _prepare_base_cmd(): |
| 58 |
if module.params["timeout"]: |
| 59 |
_tout_cmd = module.get_bin_path("timeout", False) |
| 60 |
if _tout_cmd: |
| 61 |
return ["timeout", "-s", "9", module.params["timeout"], _puppet_cmd(module)] |
| 62 |
return ["puppet"] |
| 63 |
|
| 64 |
def noop_func(v): |
| 65 |
return ["--noop"] if module.check_mode or v else ["--no-noop"] |
| 66 |
|
| 67 |
_logdest_map = { |
| 68 |
"syslog": ["--logdest", "syslog"], |
| 69 |
"all": ["--logdest", "syslog", "--logdest", "console"], |
| 70 |
} |
| 71 |
|
| 72 |
@cmd_runner_fmt.unpack_args |
| 73 |
def execute_func(execute, manifest): |
| 74 |
if execute: |
| 75 |
return ["--execute", execute] |
| 76 |
else: |
| 77 |
return [manifest] |
| 78 |
|
| 79 |
runner = CmdRunner( |
| 80 |
module, |
| 81 |
command=_prepare_base_cmd(), |
| 82 |
path_prefix=_PUPPET_PATH_PREFIX, |
| 83 |
arg_formats=dict( |
| 84 |
_agent_fixed=cmd_runner_fmt.as_fixed( |
| 85 |
[ |
| 86 |
"agent", |
| 87 |
"--onetime", |
| 88 |
"--no-daemonize", |
| 89 |
"--no-usecacheonfailure", |
| 90 |
"--no-splay", |
| 91 |
"--detailed-exitcodes", |
| 92 |
"--verbose", |
| 93 |
"--color", |
| 94 |
"0", |
| 95 |
] |
| 96 |
), |
| 97 |
_apply_fixed=cmd_runner_fmt.as_fixed(["apply", "--detailed-exitcodes"]), |
| 98 |
puppetmaster=cmd_runner_fmt.as_opt_val("--server"), |
| 99 |
show_diff=cmd_runner_fmt.as_bool("--show-diff"), |
| 100 |
confdir=cmd_runner_fmt.as_opt_val("--confdir"), |
| 101 |
environment=cmd_runner_fmt.as_opt_val("--environment"), |
| 102 |
tags=cmd_runner_fmt.as_func(lambda v: ["--tags", ",".join(v)]), |
| 103 |
skip_tags=cmd_runner_fmt.as_func(lambda v: ["--skip_tags", ",".join(v)]), |
| 104 |
certname=cmd_runner_fmt.as_opt_eq_val("--certname"), |
| 105 |
noop=cmd_runner_fmt.as_func(noop_func), |
| 106 |
use_srv_records=cmd_runner_fmt.as_bool("--usr_srv_records", "--no-usr_srv_records", ignore_none=True), |
| 107 |
logdest=cmd_runner_fmt.as_map(_logdest_map, default=[]), |
| 108 |
modulepath=cmd_runner_fmt.as_opt_eq_val("--modulepath"), |
| 109 |
_execute=cmd_runner_fmt.as_func(execute_func), |
| 110 |
summarize=cmd_runner_fmt.as_bool("--summarize"), |
| 111 |
waitforlock=cmd_runner_fmt.as_opt_val("--waitforlock"), |
| 112 |
debug=cmd_runner_fmt.as_bool("--debug"), |
| 113 |
verbose=cmd_runner_fmt.as_bool("--verbose"), |
| 114 |
), |
| 115 |
check_rc=False, |
| 116 |
force_lang=module.params["environment_lang"], |
| 117 |
) |
| 118 |
return runner |