2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2015, Hewlett-Packard Development Company, L.P.
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = r'''
|
|
|
|
---
|
|
|
|
module: puppet
|
|
|
|
short_description: Runs puppet
|
|
|
|
description:
|
2023-06-15 15:48:17 +02:00
|
|
|
- Runs C(puppet) agent or apply in a reliable manner.
|
2023-02-20 17:29:14 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
timeout:
|
|
|
|
description:
|
2023-06-15 15:48:17 +02:00
|
|
|
- How long to wait for C(puppet) to finish.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
default: 30m
|
|
|
|
puppetmaster:
|
|
|
|
description:
|
|
|
|
- The hostname of the puppetmaster to contact.
|
|
|
|
type: str
|
|
|
|
modulepath:
|
|
|
|
description:
|
|
|
|
- Path to an alternate location for puppet modules.
|
|
|
|
type: str
|
|
|
|
manifest:
|
|
|
|
description:
|
|
|
|
- Path to the manifest file to run puppet apply on.
|
|
|
|
type: str
|
|
|
|
noop:
|
|
|
|
description:
|
|
|
|
- Override puppet.conf noop mode.
|
2023-06-15 15:48:17 +02:00
|
|
|
- When V(true), run Puppet agent with C(--noop) switch set.
|
|
|
|
- When V(false), run Puppet agent with C(--no-noop) switch set.
|
2020-03-09 10:11:07 +01:00
|
|
|
- When unset (default), use default or puppet.conf value if defined.
|
|
|
|
type: bool
|
|
|
|
facts:
|
|
|
|
description:
|
|
|
|
- A dict of values to pass in as persistent external facter facts.
|
|
|
|
type: dict
|
|
|
|
facter_basename:
|
|
|
|
description:
|
|
|
|
- Basename of the facter output file.
|
|
|
|
type: str
|
|
|
|
default: ansible
|
|
|
|
environment:
|
|
|
|
description:
|
|
|
|
- Puppet environment to be used.
|
|
|
|
type: str
|
2022-06-06 10:32:20 +02:00
|
|
|
confdir:
|
|
|
|
description:
|
|
|
|
- Path to the directory containing the puppet.conf file.
|
|
|
|
type: str
|
|
|
|
version_added: 5.1.0
|
2020-03-09 10:11:07 +01:00
|
|
|
logdest:
|
|
|
|
description:
|
|
|
|
- Where the puppet logs should go, if puppet apply is being used.
|
2023-06-15 15:48:17 +02:00
|
|
|
- V(all) will go to both C(console) and C(syslog).
|
|
|
|
- V(stdout) will be deprecated and replaced by C(console).
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
choices: [ all, stdout, syslog ]
|
|
|
|
default: stdout
|
|
|
|
certname:
|
|
|
|
description:
|
|
|
|
- The name to use when handling certificates.
|
|
|
|
type: str
|
|
|
|
tags:
|
|
|
|
description:
|
|
|
|
- A list of puppet tags to be used.
|
|
|
|
type: list
|
2020-11-04 09:02:50 +01:00
|
|
|
elements: str
|
2023-04-13 06:40:54 +02:00
|
|
|
skip_tags:
|
|
|
|
description:
|
|
|
|
- A list of puppet tags to be excluded.
|
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
version_added: 6.6.0
|
2020-03-09 10:11:07 +01:00
|
|
|
execute:
|
|
|
|
description:
|
|
|
|
- Execute a specific piece of Puppet code.
|
|
|
|
- It has no effect with a puppetmaster.
|
|
|
|
type: str
|
|
|
|
use_srv_records:
|
|
|
|
description:
|
|
|
|
- Toggles use_srv_records flag
|
|
|
|
type: bool
|
|
|
|
summarize:
|
|
|
|
description:
|
|
|
|
- Whether to print a transaction summary.
|
|
|
|
type: bool
|
2020-11-23 12:14:43 +01:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
verbose:
|
|
|
|
description:
|
|
|
|
- Print extra information.
|
|
|
|
type: bool
|
2020-11-23 12:14:43 +01:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
debug:
|
|
|
|
description:
|
|
|
|
- Enable full debugging.
|
|
|
|
type: bool
|
2020-11-23 12:14:43 +01:00
|
|
|
default: false
|
2022-01-10 07:08:17 +01:00
|
|
|
show_diff:
|
|
|
|
description:
|
|
|
|
- Whether to print file changes details
|
|
|
|
type: bool
|
|
|
|
default: false
|
2024-04-20 10:20:55 +02:00
|
|
|
environment_lang:
|
|
|
|
description:
|
|
|
|
- The lang environment to use when running the puppet agent.
|
|
|
|
- The default value, V(C), is supported on every system, but can lead to encoding errors if UTF-8 is used in the output
|
|
|
|
- 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 selected locale is supported on the system the puppet agent runs on.
|
|
|
|
type: str
|
|
|
|
default: C
|
|
|
|
version_added: 8.6.0
|
2020-03-09 10:11:07 +01:00
|
|
|
requirements:
|
|
|
|
- puppet
|
|
|
|
author:
|
|
|
|
- Monty Taylor (@emonty)
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = r'''
|
|
|
|
- name: Run puppet agent and fail if anything goes wrong
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.puppet:
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
- name: Run puppet and timeout in 5 minutes
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.puppet:
|
2020-03-09 10:11:07 +01:00
|
|
|
timeout: 5m
|
|
|
|
|
|
|
|
- name: Run puppet using a different environment
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.puppet:
|
2020-03-09 10:11:07 +01:00
|
|
|
environment: testing
|
|
|
|
|
|
|
|
- name: Run puppet using a specific certname
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.puppet:
|
2020-03-09 10:11:07 +01:00
|
|
|
certname: agent01.example.com
|
|
|
|
|
|
|
|
- name: Run puppet using a specific piece of Puppet code. Has no effect with a puppetmaster
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.puppet:
|
2020-03-09 10:11:07 +01:00
|
|
|
execute: include ::mymodule
|
|
|
|
|
|
|
|
- name: Run puppet using a specific tags
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.puppet:
|
2020-03-09 10:11:07 +01:00
|
|
|
tags:
|
|
|
|
- update
|
|
|
|
- nginx
|
2023-04-13 06:40:54 +02:00
|
|
|
skip_tags:
|
|
|
|
- service
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
- name: Run puppet agent in noop mode
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.puppet:
|
2022-08-24 19:59:01 +02:00
|
|
|
noop: true
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-05-03 07:26:47 +02:00
|
|
|
- name: Run a manifest with debug, log to both syslog and console, specify module path
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.puppet:
|
2020-03-09 10:11:07 +01:00
|
|
|
modulepath: /etc/puppet/modules:/opt/stack/puppet-modules:/usr/share/openstack-puppet/modules
|
|
|
|
logdest: all
|
|
|
|
manifest: /var/lib/example/puppet_step_config.pp
|
|
|
|
'''
|
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import stat
|
|
|
|
|
2022-12-14 21:30:03 +01:00
|
|
|
import ansible_collections.community.general.plugins.module_utils.puppet as puppet_utils
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-12-14 21:30:03 +01:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _write_structured_data(basedir, basename, data):
|
|
|
|
if not os.path.exists(basedir):
|
|
|
|
os.makedirs(basedir)
|
|
|
|
file_path = os.path.join(basedir, "{0}.json".format(basename))
|
|
|
|
# This is more complex than you might normally expect because we want to
|
|
|
|
# open the file with only u+rw set. Also, we use the stat constants
|
|
|
|
# because ansible still supports python 2.4 and the octal syntax changed
|
|
|
|
out_file = os.fdopen(
|
|
|
|
os.open(
|
|
|
|
file_path, os.O_CREAT | os.O_WRONLY,
|
|
|
|
stat.S_IRUSR | stat.S_IWUSR), 'w')
|
|
|
|
out_file.write(json.dumps(data).encode('utf8'))
|
|
|
|
out_file.close()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
timeout=dict(type='str', default='30m'),
|
|
|
|
puppetmaster=dict(type='str'),
|
|
|
|
modulepath=dict(type='str'),
|
|
|
|
manifest=dict(type='str'),
|
2022-06-06 10:32:20 +02:00
|
|
|
confdir=dict(type='str'),
|
2021-03-05 08:17:36 +01:00
|
|
|
noop=dict(type='bool'),
|
|
|
|
logdest=dict(type='str', default='stdout', choices=['all', 'stdout', 'syslog']),
|
2022-01-10 07:08:17 +01:00
|
|
|
# The following is not related to Ansible's diff; see https://github.com/ansible-collections/community.general/pull/3980#issuecomment-1005666154
|
2023-04-26 07:32:00 +02:00
|
|
|
show_diff=dict(type='bool', default=False),
|
2020-03-09 10:11:07 +01:00
|
|
|
facts=dict(type='dict'),
|
|
|
|
facter_basename=dict(type='str', default='ansible'),
|
|
|
|
environment=dict(type='str'),
|
|
|
|
certname=dict(type='str'),
|
2020-11-04 09:02:50 +01:00
|
|
|
tags=dict(type='list', elements='str'),
|
2023-04-13 06:40:54 +02:00
|
|
|
skip_tags=dict(type='list', elements='str'),
|
2020-03-09 10:11:07 +01:00
|
|
|
execute=dict(type='str'),
|
|
|
|
summarize=dict(type='bool', default=False),
|
|
|
|
debug=dict(type='bool', default=False),
|
|
|
|
verbose=dict(type='bool', default=False),
|
|
|
|
use_srv_records=dict(type='bool'),
|
2024-04-20 10:20:55 +02:00
|
|
|
environment_lang=dict(type='str', default='C'),
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
|
|
|
mutually_exclusive=[
|
|
|
|
('puppetmaster', 'manifest'),
|
|
|
|
('puppetmaster', 'manifest', 'execute'),
|
|
|
|
('puppetmaster', 'modulepath'),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
p = module.params
|
|
|
|
|
|
|
|
if p['manifest']:
|
|
|
|
if not os.path.exists(p['manifest']):
|
|
|
|
module.fail_json(
|
|
|
|
msg="Manifest file %(manifest)s not found." % dict(
|
|
|
|
manifest=p['manifest']))
|
|
|
|
|
|
|
|
# Check if puppet is disabled here
|
|
|
|
if not p['manifest']:
|
2022-12-14 21:30:03 +01:00
|
|
|
puppet_utils.ensure_agent_enabled(module)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if module.params['facts'] and not module.check_mode:
|
|
|
|
_write_structured_data(
|
2022-12-14 21:30:03 +01:00
|
|
|
puppet_utils.get_facter_dir(),
|
2020-03-09 10:11:07 +01:00
|
|
|
module.params['facter_basename'],
|
|
|
|
module.params['facts'])
|
|
|
|
|
2022-12-14 21:30:03 +01:00
|
|
|
runner = puppet_utils.puppet_runner(module)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if not p['manifest'] and not p['execute']:
|
2023-04-13 06:40:54 +02:00
|
|
|
args_order = "_agent_fixed puppetmaster show_diff confdir environment tags skip_tags certname noop use_srv_records"
|
2022-12-14 21:30:03 +01:00
|
|
|
with runner(args_order) as ctx:
|
|
|
|
rc, stdout, stderr = ctx.run()
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2023-04-13 06:40:54 +02:00
|
|
|
args_order = "_apply_fixed logdest modulepath environment certname tags skip_tags noop _execute summarize debug verbose"
|
2022-12-14 21:30:03 +01:00
|
|
|
with runner(args_order) as ctx:
|
|
|
|
rc, stdout, stderr = ctx.run(_execute=[p['execute'], p['manifest']])
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if rc == 0:
|
|
|
|
# success
|
|
|
|
module.exit_json(rc=rc, changed=False, stdout=stdout, stderr=stderr)
|
|
|
|
elif rc == 1:
|
|
|
|
# rc==1 could be because it's disabled
|
|
|
|
# rc==1 could also mean there was a compilation failure
|
|
|
|
disabled = "administratively disabled" in stdout
|
|
|
|
if disabled:
|
|
|
|
msg = "puppet is disabled"
|
|
|
|
else:
|
|
|
|
msg = "puppet did not run"
|
|
|
|
module.exit_json(
|
|
|
|
rc=rc, disabled=disabled, msg=msg,
|
|
|
|
error=True, stdout=stdout, stderr=stderr)
|
|
|
|
elif rc == 2:
|
|
|
|
# success with changes
|
|
|
|
module.exit_json(rc=0, changed=True, stdout=stdout, stderr=stderr)
|
|
|
|
elif rc == 124:
|
|
|
|
# timeout
|
|
|
|
module.exit_json(
|
2022-12-14 21:30:03 +01:00
|
|
|
rc=rc, msg="%s timed out" % ctx.cmd, stdout=stdout, stderr=stderr)
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
|
|
|
# failure
|
|
|
|
module.fail_json(
|
2022-12-14 21:30:03 +01:00
|
|
|
rc=rc, msg="%s failed with return code: %d" % (ctx.cmd, rc),
|
2020-03-09 10:11:07 +01:00
|
|
|
stdout=stdout, stderr=stderr)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|