mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-23 19:01:26 -07:00
[PR #5612/f95e0d77 backport][stable-6] puppet: refactored to use CmdRunner (#5686)
puppet: refactored to use CmdRunner (#5612)
* puppet: refactored to use CmdRunner
* add changelog fragment
* add more tests
(cherry picked from commit f95e0d775d
)
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
parent
6f845f61f0
commit
1489c080a7
10 changed files with 282 additions and 105 deletions
|
@ -152,15 +152,9 @@ import json
|
|||
import os
|
||||
import stat
|
||||
|
||||
import ansible_collections.community.general.plugins.module_utils.puppet as puppet_utils
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.six.moves import shlex_quote
|
||||
|
||||
|
||||
def _get_facter_dir():
|
||||
if os.getuid() == 0:
|
||||
return '/etc/facter/facts.d'
|
||||
else:
|
||||
return os.path.expanduser('~/.facter/facts.d')
|
||||
|
||||
|
||||
def _write_structured_data(basedir, basename, data):
|
||||
|
@ -212,16 +206,6 @@ def main():
|
|||
)
|
||||
p = module.params
|
||||
|
||||
global PUPPET_CMD
|
||||
PUPPET_CMD = module.get_bin_path("puppet", False, ['/opt/puppetlabs/bin'])
|
||||
|
||||
if not PUPPET_CMD:
|
||||
module.fail_json(
|
||||
msg="Could not find puppet. Please ensure it is installed.")
|
||||
|
||||
global TIMEOUT_CMD
|
||||
TIMEOUT_CMD = module.get_bin_path("timeout", False)
|
||||
|
||||
if p['manifest']:
|
||||
if not os.path.exists(p['manifest']):
|
||||
module.fail_json(
|
||||
|
@ -230,90 +214,24 @@ def main():
|
|||
|
||||
# Check if puppet is disabled here
|
||||
if not p['manifest']:
|
||||
rc, stdout, stderr = module.run_command(
|
||||
PUPPET_CMD + " config print agent_disabled_lockfile")
|
||||
if os.path.exists(stdout.strip()):
|
||||
module.fail_json(
|
||||
msg="Puppet agent is administratively disabled.",
|
||||
disabled=True)
|
||||
elif rc != 0:
|
||||
module.fail_json(
|
||||
msg="Puppet agent state could not be determined.")
|
||||
puppet_utils.ensure_agent_enabled(module)
|
||||
|
||||
if module.params['facts'] and not module.check_mode:
|
||||
_write_structured_data(
|
||||
_get_facter_dir(),
|
||||
puppet_utils.get_facter_dir(),
|
||||
module.params['facter_basename'],
|
||||
module.params['facts'])
|
||||
|
||||
if TIMEOUT_CMD:
|
||||
base_cmd = "%(timeout_cmd)s -s 9 %(timeout)s %(puppet_cmd)s" % dict(
|
||||
timeout_cmd=TIMEOUT_CMD,
|
||||
timeout=shlex_quote(p['timeout']),
|
||||
puppet_cmd=PUPPET_CMD)
|
||||
else:
|
||||
base_cmd = PUPPET_CMD
|
||||
runner = puppet_utils.puppet_runner(module)
|
||||
|
||||
if not p['manifest'] and not p['execute']:
|
||||
cmd = ("%(base_cmd)s agent --onetime"
|
||||
" --no-daemonize --no-usecacheonfailure --no-splay"
|
||||
" --detailed-exitcodes --verbose --color 0") % dict(base_cmd=base_cmd)
|
||||
if p['puppetmaster']:
|
||||
cmd += " --server %s" % shlex_quote(p['puppetmaster'])
|
||||
if p['show_diff']:
|
||||
cmd += " --show_diff"
|
||||
if p['confdir']:
|
||||
cmd += " --confdir %s" % shlex_quote(p['confdir'])
|
||||
if p['environment']:
|
||||
cmd += " --environment '%s'" % p['environment']
|
||||
if p['tags']:
|
||||
cmd += " --tags '%s'" % ','.join(p['tags'])
|
||||
if p['certname']:
|
||||
cmd += " --certname='%s'" % p['certname']
|
||||
if module.check_mode:
|
||||
cmd += " --noop"
|
||||
elif 'noop' in p:
|
||||
if p['noop']:
|
||||
cmd += " --noop"
|
||||
else:
|
||||
cmd += " --no-noop"
|
||||
if p['use_srv_records'] is not None:
|
||||
if not p['use_srv_records']:
|
||||
cmd += " --no-use_srv_records"
|
||||
else:
|
||||
cmd += " --use_srv_records"
|
||||
args_order = "_agent_fixed puppetmaster show_diff confdir environment tags certname noop use_srv_records"
|
||||
with runner(args_order) as ctx:
|
||||
rc, stdout, stderr = ctx.run()
|
||||
else:
|
||||
cmd = "%s apply --detailed-exitcodes " % base_cmd
|
||||
if p['logdest'] == 'syslog':
|
||||
cmd += "--logdest syslog "
|
||||
if p['logdest'] == 'all':
|
||||
cmd += " --logdest syslog --logdest console"
|
||||
if p['modulepath']:
|
||||
cmd += "--modulepath='%s'" % p['modulepath']
|
||||
if p['environment']:
|
||||
cmd += "--environment '%s' " % p['environment']
|
||||
if p['certname']:
|
||||
cmd += " --certname='%s'" % p['certname']
|
||||
if p['tags']:
|
||||
cmd += " --tags '%s'" % ','.join(p['tags'])
|
||||
if module.check_mode:
|
||||
cmd += "--noop "
|
||||
elif 'noop' in p:
|
||||
if p['noop']:
|
||||
cmd += " --noop"
|
||||
else:
|
||||
cmd += " --no-noop"
|
||||
if p['execute']:
|
||||
cmd += " --execute '%s'" % p['execute']
|
||||
else:
|
||||
cmd += " %s" % shlex_quote(p['manifest'])
|
||||
if p['summarize']:
|
||||
cmd += " --summarize"
|
||||
if p['debug']:
|
||||
cmd += " --debug"
|
||||
if p['verbose']:
|
||||
cmd += " --verbose"
|
||||
rc, stdout, stderr = module.run_command(cmd)
|
||||
args_order = "_apply_fixed logdest modulepath environment certname tags noop _execute summarize debug verbose"
|
||||
with runner(args_order) as ctx:
|
||||
rc, stdout, stderr = ctx.run(_execute=[p['execute'], p['manifest']])
|
||||
|
||||
if rc == 0:
|
||||
# success
|
||||
|
@ -335,11 +253,11 @@ def main():
|
|||
elif rc == 124:
|
||||
# timeout
|
||||
module.exit_json(
|
||||
rc=rc, msg="%s timed out" % cmd, stdout=stdout, stderr=stderr)
|
||||
rc=rc, msg="%s timed out" % ctx.cmd, stdout=stdout, stderr=stderr)
|
||||
else:
|
||||
# failure
|
||||
module.fail_json(
|
||||
rc=rc, msg="%s failed with return code: %d" % (cmd, rc),
|
||||
rc=rc, msg="%s failed with return code: %d" % (ctx.cmd, rc),
|
||||
stdout=stdout, stderr=stderr)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue