| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2015, Hewlett-Packard Development Company, L.P. |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: puppet |
| 11 |
short_description: Runs puppet |
| 12 |
description: |
| 13 |
- Runs C(puppet) agent or apply in a reliable manner. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
timeout: |
| 23 |
description: |
| 24 |
- How long to wait for C(puppet) to finish. |
| 25 |
- This parameter is deprecated and will be removed in community.general 14.0.0. |
| 26 |
Use the task-level C(timeout) keyword instead. |
| 27 |
To suppress this deprecation warning, set O(timeout) to V(""). |
| 28 |
type: str |
| 29 |
default: 30m |
| 30 |
puppetmaster: |
| 31 |
description: |
| 32 |
- The hostname of the puppetmaster to contact. |
| 33 |
type: str |
| 34 |
modulepath: |
| 35 |
description: |
| 36 |
- Path to an alternate location for puppet modules. |
| 37 |
type: str |
| 38 |
manifest: |
| 39 |
description: |
| 40 |
- Path to the manifest file to run puppet apply on. |
| 41 |
type: str |
| 42 |
noop: |
| 43 |
description: |
| 44 |
- Override puppet.conf noop mode. |
| 45 |
- When V(true), run Puppet agent with C(--noop) switch set. |
| 46 |
- When V(false), run Puppet agent with C(--no-noop) switch set. |
| 47 |
- When unset (default), use default or puppet.conf value if defined. |
| 48 |
type: bool |
| 49 |
facts: |
| 50 |
description: |
| 51 |
- A dict of values to pass in as persistent external facter facts. |
| 52 |
type: dict |
| 53 |
facter_basename: |
| 54 |
description: |
| 55 |
- Basename of the facter output file. |
| 56 |
type: str |
| 57 |
default: ansible |
| 58 |
environment: |
| 59 |
description: |
| 60 |
- Puppet environment to be used. |
| 61 |
type: str |
| 62 |
confdir: |
| 63 |
description: |
| 64 |
- Path to the directory containing the puppet.conf file. |
| 65 |
type: str |
| 66 |
version_added: 5.1.0 |
| 67 |
logdest: |
| 68 |
description: |
| 69 |
- Where the puppet logs should go, if puppet apply is being used. |
| 70 |
- V(all) goes to both C(console) and C(syslog). |
| 71 |
- V(stdout) is deprecated and replaced by C(console). # TODO which/where is console? |
| 72 |
type: str |
| 73 |
choices: [all, stdout, syslog] |
| 74 |
default: stdout |
| 75 |
certname: |
| 76 |
description: |
| 77 |
- The name to use when handling certificates. |
| 78 |
type: str |
| 79 |
tags: |
| 80 |
description: |
| 81 |
- A list of puppet tags to be used. |
| 82 |
type: list |
| 83 |
elements: str |
| 84 |
skip_tags: |
| 85 |
description: |
| 86 |
- A list of puppet tags to be excluded. |
| 87 |
type: list |
| 88 |
elements: str |
| 89 |
version_added: 6.6.0 |
| 90 |
execute: |
| 91 |
description: |
| 92 |
- Execute a specific piece of Puppet code. |
| 93 |
- It has no effect with a puppetmaster. |
| 94 |
type: str |
| 95 |
use_srv_records: |
| 96 |
description: |
| 97 |
- Toggles use_srv_records flag. |
| 98 |
type: bool |
| 99 |
summarize: |
| 100 |
description: |
| 101 |
- Whether to print a transaction summary. |
| 102 |
type: bool |
| 103 |
default: false |
| 104 |
waitforlock: |
| 105 |
description: |
| 106 |
- The maximum amount of time C(puppet) should wait for an already running C(puppet) agent to finish before starting. |
| 107 |
- If a number without unit is provided, it is assumed to be a number of seconds. Allowed units are V(m) for minutes |
| 108 |
and V(h) for hours. |
| 109 |
type: str |
| 110 |
version_added: 9.0.0 |
| 111 |
verbose: |
| 112 |
description: |
| 113 |
- Print extra information. |
| 114 |
type: bool |
| 115 |
default: false |
| 116 |
debug: |
| 117 |
description: |
| 118 |
- Enable full debugging. |
| 119 |
type: bool |
| 120 |
default: false |
| 121 |
show_diff: |
| 122 |
description: |
| 123 |
- Whether to print file changes details. |
| 124 |
type: bool |
| 125 |
default: false |
| 126 |
environment_lang: |
| 127 |
description: |
| 128 |
- The lang environment to use when running the puppet agent. |
| 129 |
- The default value, V(C), is supported on every system, but can lead to encoding errors if UTF-8 is used in the output. |
| 130 |
- Use V(C.UTF-8) or V(en_US.UTF-8) or similar UTF-8 supporting locales in case of problems. You need to make sure the |
| 131 |
selected locale is supported on the system the puppet agent runs on. |
| 132 |
- Starting with community.general 9.1.0, you can use the value V(auto) and the module tries to determine the best parseable |
| 133 |
locale to use. |
| 134 |
type: str |
| 135 |
default: C |
| 136 |
version_added: 8.6.0 |
| 137 |
requirements: |
| 138 |
- puppet |
| 139 |
author: |
| 140 |
- Monty Taylor (@emonty) |
| 141 |
""" |
| 142 |
|
| 143 |
EXAMPLES = r""" |
| 144 |
- name: Run puppet agent and fail if anything goes wrong |
| 145 |
community.general.puppet: |
| 146 |
|
| 147 |
- name: Run puppet and timeout in 5 minutes |
| 148 |
community.general.puppet: |
| 149 |
timeout: 5m |
| 150 |
|
| 151 |
- name: Run puppet using a different environment |
| 152 |
community.general.puppet: |
| 153 |
environment: testing |
| 154 |
|
| 155 |
- name: Run puppet using a specific certname |
| 156 |
community.general.puppet: |
| 157 |
certname: agent01.example.com |
| 158 |
|
| 159 |
- name: Run puppet using a specific piece of Puppet code. Has no effect with a puppetmaster |
| 160 |
community.general.puppet: |
| 161 |
execute: include ::mymodule |
| 162 |
|
| 163 |
- name: Run puppet using a specific tags |
| 164 |
community.general.puppet: |
| 165 |
tags: |
| 166 |
- update |
| 167 |
- nginx |
| 168 |
skip_tags: |
| 169 |
- service |
| 170 |
|
| 171 |
- name: Wait 30 seconds for any current puppet runs to finish |
| 172 |
community.general.puppet: |
| 173 |
waitforlock: 30 |
| 174 |
|
| 175 |
- name: Wait 5 minutes for any current puppet runs to finish |
| 176 |
community.general.puppet: |
| 177 |
waitforlock: 5m |
| 178 |
|
| 179 |
- name: Run puppet agent in noop mode |
| 180 |
community.general.puppet: |
| 181 |
noop: true |
| 182 |
|
| 183 |
- name: Run a manifest with debug, log to both syslog and console, specify module path |
| 184 |
community.general.puppet: |
| 185 |
modulepath: /etc/puppet/modules:/opt/stack/puppet-modules:/usr/share/openstack-puppet/modules |
| 186 |
logdest: all |
| 187 |
manifest: /var/lib/example/puppet_step_config.pp |
| 188 |
""" |
| 189 |
|
| 190 |
import json |
| 191 |
import os |
| 192 |
import stat |
| 193 |
|
| 194 |
from ansible.module_utils.basic import AnsibleModule |
| 195 |
|
| 196 |
import ansible_collections.community.general.plugins.module_utils._puppet as puppet_utils |
| 197 |
|
| 198 |
|
| 199 |
def _write_structured_data(basedir, basename, data): |
| 200 |
if not os.path.exists(basedir): |
| 201 |
os.makedirs(basedir) |
| 202 |
file_path = os.path.join(basedir, f"{basename}.json") |
| 203 |
# This is more complex than you might normally expect because we want to |
| 204 |
# open the file with only u+rw set. Also, we use the stat constants |
| 205 |
# because ansible still supports python 2.4 and the octal syntax changed |
| 206 |
out_file = os.fdopen(os.open(file_path, os.O_CREAT | os.O_WRONLY, stat.S_IRUSR | stat.S_IWUSR), "w") |
| 207 |
out_file.write(json.dumps(data)) |
| 208 |
out_file.close() |
| 209 |
|
| 210 |
|
| 211 |
def main(): |
| 212 |
module = AnsibleModule( |
| 213 |
argument_spec=dict( |
| 214 |
timeout=dict(type="str", default="30m"), |
| 215 |
puppetmaster=dict(type="str"), |
| 216 |
modulepath=dict(type="str"), |
| 217 |
manifest=dict(type="str"), |
| 218 |
confdir=dict(type="str"), |
| 219 |
noop=dict(type="bool"), |
| 220 |
logdest=dict(type="str", default="stdout", choices=["all", "stdout", "syslog"]), |
| 221 |
# The following is not related to Ansible's diff; see https://github.com/ansible-collections/community.general/pull/3980#issuecomment-1005666154 |
| 222 |
show_diff=dict(type="bool", default=False), |
| 223 |
facts=dict(type="dict"), |
| 224 |
facter_basename=dict(type="str", default="ansible"), |
| 225 |
environment=dict(type="str"), |
| 226 |
certname=dict(type="str"), |
| 227 |
tags=dict(type="list", elements="str"), |
| 228 |
skip_tags=dict(type="list", elements="str"), |
| 229 |
execute=dict(type="str"), |
| 230 |
summarize=dict(type="bool", default=False), |
| 231 |
waitforlock=dict(type="str"), |
| 232 |
debug=dict(type="bool", default=False), |
| 233 |
verbose=dict(type="bool", default=False), |
| 234 |
use_srv_records=dict(type="bool"), |
| 235 |
environment_lang=dict(type="str", default="C"), |
| 236 |
), |
| 237 |
supports_check_mode=True, |
| 238 |
mutually_exclusive=[ |
| 239 |
("puppetmaster", "manifest"), |
| 240 |
("puppetmaster", "manifest", "execute"), |
| 241 |
("puppetmaster", "modulepath"), |
| 242 |
], |
| 243 |
) |
| 244 |
p = module.params |
| 245 |
|
| 246 |
if p["timeout"]: |
| 247 |
module.deprecate( |
| 248 |
'The "timeout" parameter is deprecated and will be removed in community.general 14.0.0. ' |
| 249 |
'Use the task-level "timeout" keyword instead. ' |
| 250 |
"To suppress this warning, set \"timeout: ''\" in the task.", |
| 251 |
version="14.0.0", |
| 252 |
collection_name="community.general", |
| 253 |
) |
| 254 |
|
| 255 |
if p["manifest"]: |
| 256 |
if not os.path.exists(p["manifest"]): |
| 257 |
module.fail_json(msg=f"Manifest file {dict(manifest=p['manifest'])['manifest']} not found.") |
| 258 |
|
| 259 |
# Check if puppet is disabled here |
| 260 |
if not p["manifest"]: |
| 261 |
puppet_utils.ensure_agent_enabled(module) |
| 262 |
|
| 263 |
if module.params["facts"] and not module.check_mode: |
| 264 |
_write_structured_data(puppet_utils.get_facter_dir(), module.params["facter_basename"], module.params["facts"]) |
| 265 |
|
| 266 |
runner = puppet_utils.puppet_runner(module) |
| 267 |
|
| 268 |
if not p["manifest"] and not p["execute"]: |
| 269 |
args_order = "_agent_fixed puppetmaster show_diff confdir environment tags skip_tags certname noop use_srv_records waitforlock" |
| 270 |
with runner(args_order) as ctx: |
| 271 |
rc, stdout, stderr = ctx.run() |
| 272 |
else: |
| 273 |
args_order = "_apply_fixed logdest modulepath environment certname tags skip_tags noop _execute summarize debug verbose waitforlock" |
| 274 |
with runner(args_order) as ctx: |
| 275 |
rc, stdout, stderr = ctx.run(_execute=[p["execute"], p["manifest"]]) |
| 276 |
|
| 277 |
if rc == 0: |
| 278 |
# success |
| 279 |
module.exit_json(rc=rc, changed=False, stdout=stdout, stderr=stderr) |
| 280 |
elif rc == 1: |
| 281 |
# rc==1 could be because it is disabled |
| 282 |
# rc==1 could also mean there was a compilation failure |
| 283 |
disabled = "administratively disabled" in stdout |
| 284 |
if disabled: |
| 285 |
msg = "puppet is disabled" |
| 286 |
else: |
| 287 |
msg = "puppet did not run" |
| 288 |
module.exit_json(rc=rc, disabled=disabled, msg=msg, error=True, stdout=stdout, stderr=stderr) |
| 289 |
elif rc == 2: |
| 290 |
# success with changes |
| 291 |
module.exit_json(rc=0, changed=True, stdout=stdout, stderr=stderr) |
| 292 |
elif rc == 124: |
| 293 |
# timeout |
| 294 |
module.exit_json(rc=rc, msg=f"{ctx.cmd} timed out", stdout=stdout, stderr=stderr) |
| 295 |
else: |
| 296 |
# failure |
| 297 |
module.fail_json(rc=rc, msg=f"{ctx.cmd} failed with return code: {rc}", stdout=stdout, stderr=stderr) |
| 298 |
|
| 299 |
|
| 300 |
if __name__ == "__main__": |
| 301 |
main() |