2015-05-28 22:23:27 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
|
|
|
|
#
|
|
|
|
# This module is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This software is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this software. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import pipes
|
2015-05-29 14:09:31 +02:00
|
|
|
import stat
|
2015-05-28 22:23:27 +02:00
|
|
|
|
2015-11-25 13:44:32 +01:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
2016-01-11 21:47:21 +01:00
|
|
|
try:
|
|
|
|
import simplejson as json
|
|
|
|
except ImportError:
|
|
|
|
# Let snippet from module_utils/basic.py return a proper error in this case
|
|
|
|
pass
|
|
|
|
|
2015-11-25 13:44:32 +01:00
|
|
|
|
2015-05-28 22:23:27 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: puppet
|
|
|
|
short_description: Runs puppet
|
|
|
|
description:
|
|
|
|
- Runs I(puppet) agent or apply in a reliable manner
|
|
|
|
version_added: "2.0"
|
|
|
|
options:
|
|
|
|
timeout:
|
|
|
|
description:
|
|
|
|
- How long to wait for I(puppet) to finish.
|
|
|
|
required: false
|
|
|
|
default: 30m
|
|
|
|
puppetmaster:
|
|
|
|
description:
|
2015-06-06 10:08:16 +02:00
|
|
|
- The hostname of the puppetmaster to contact.
|
2015-05-28 22:23:27 +02:00
|
|
|
required: false
|
|
|
|
default: None
|
|
|
|
manifest:
|
2015-11-25 13:44:32 +01:00
|
|
|
description:
|
2015-06-06 10:08:16 +02:00
|
|
|
- Path to the manifest file to run puppet apply on.
|
2015-05-28 22:23:27 +02:00
|
|
|
required: false
|
|
|
|
default: None
|
|
|
|
facts:
|
|
|
|
description:
|
|
|
|
- A dict of values to pass in as persistent external facter facts
|
|
|
|
required: false
|
|
|
|
default: None
|
|
|
|
facter_basename:
|
2015-08-10 19:25:13 +02:00
|
|
|
description:
|
2015-05-28 22:23:27 +02:00
|
|
|
- Basename of the facter output file
|
|
|
|
required: false
|
|
|
|
default: ansible
|
2015-06-06 09:42:56 +02:00
|
|
|
environment:
|
2015-08-10 19:25:13 +02:00
|
|
|
description:
|
2015-06-06 09:42:56 +02:00
|
|
|
- Puppet environment to be used.
|
|
|
|
required: false
|
|
|
|
default: None
|
2015-05-28 22:23:27 +02:00
|
|
|
requirements: [ puppet ]
|
2015-06-10 13:00:02 +02:00
|
|
|
author: "Monty Taylor (@emonty)"
|
2015-05-28 22:23:27 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2015-06-06 10:08:16 +02:00
|
|
|
# Run puppet agent and fail if anything goes wrong
|
2015-05-28 22:23:27 +02:00
|
|
|
- puppet
|
|
|
|
|
|
|
|
# Run puppet and timeout in 5 minutes
|
|
|
|
- puppet: timeout=5m
|
2015-06-06 09:42:56 +02:00
|
|
|
|
|
|
|
# Run puppet using a different environment
|
|
|
|
- puppet: environment=testing
|
2015-05-28 22:23:27 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
if not os.path.exists(basedir):
|
|
|
|
os.makedirs(basedir)
|
|
|
|
file_path = os.path.join(basedir, "{0}.json".format(basename))
|
2015-05-29 14:09:31 +02:00
|
|
|
# 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
|
2015-05-29 13:00:30 +02:00
|
|
|
out_file = os.fdopen(
|
2015-05-29 14:09:31 +02:00
|
|
|
os.open(
|
|
|
|
file_path, os.O_CREAT | os.O_WRONLY,
|
|
|
|
stat.S_IRUSR | stat.S_IWUSR), 'w')
|
2015-05-29 13:00:30 +02:00
|
|
|
out_file.write(json.dumps(data).encode('utf8'))
|
|
|
|
out_file.close()
|
2015-05-28 22:23:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
timeout=dict(default="30m"),
|
|
|
|
puppetmaster=dict(required=False, default=None),
|
|
|
|
manifest=dict(required=False, default=None),
|
|
|
|
show_diff=dict(
|
2016-01-06 15:49:02 +01:00
|
|
|
default=False, aliases=['show-diff'], type='bool'), # internal code to work with --diff, do not use
|
2015-05-28 22:23:27 +02:00
|
|
|
facts=dict(default=None),
|
|
|
|
facter_basename=dict(default='ansible'),
|
2015-06-06 09:42:56 +02:00
|
|
|
environment=dict(required=False, default=None),
|
2015-05-28 22:23:27 +02:00
|
|
|
),
|
2015-05-29 13:06:15 +02:00
|
|
|
supports_check_mode=True,
|
2015-06-06 10:08:16 +02:00
|
|
|
mutually_exclusive=[
|
2015-05-28 22:23:27 +02:00
|
|
|
('puppetmaster', 'manifest'),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
p = module.params
|
|
|
|
|
|
|
|
global PUPPET_CMD
|
|
|
|
PUPPET_CMD = module.get_bin_path("puppet", False)
|
|
|
|
|
|
|
|
if not PUPPET_CMD:
|
|
|
|
module.fail_json(
|
|
|
|
msg="Could not find puppet. Please ensure it is installed.")
|
|
|
|
|
2015-11-25 13:44:32 +01:00
|
|
|
global TIMEOUT_CMD
|
|
|
|
TIMEOUT_CMD = module.get_bin_path("timeout", False)
|
|
|
|
|
2015-05-28 22:23:27 +02:00
|
|
|
if p['manifest']:
|
|
|
|
if not os.path.exists(p['manifest']):
|
|
|
|
module.fail_json(
|
|
|
|
msg="Manifest file %(manifest)s not found." % dict(
|
2015-05-29 13:00:30 +02:00
|
|
|
manifest=p['manifest']))
|
2015-05-28 22:23:27 +02:00
|
|
|
|
|
|
|
# Check if puppet is disabled here
|
2015-06-06 10:08:16 +02:00
|
|
|
if not p['manifest']:
|
2015-05-28 22:23:27 +02:00
|
|
|
rc, stdout, stderr = module.run_command(
|
2015-06-06 09:46:16 +02:00
|
|
|
PUPPET_CMD + " config print agent_disabled_lockfile")
|
2015-05-28 22:23:27 +02:00
|
|
|
if os.path.exists(stdout.strip()):
|
|
|
|
module.fail_json(
|
2015-11-25 13:44:32 +01:00
|
|
|
msg="Puppet agent is administratively disabled.",
|
|
|
|
disabled=True)
|
2015-05-28 22:23:27 +02:00
|
|
|
elif rc != 0:
|
|
|
|
module.fail_json(
|
|
|
|
msg="Puppet agent state could not be determined.")
|
|
|
|
|
2015-05-29 13:06:15 +02:00
|
|
|
if module.params['facts'] and not module.check_mode:
|
2015-05-28 22:23:27 +02:00
|
|
|
_write_structured_data(
|
|
|
|
_get_facter_dir(),
|
|
|
|
module.params['facter_basename'],
|
|
|
|
module.params['facts'])
|
|
|
|
|
2015-11-25 13:44:32 +01:00
|
|
|
if TIMEOUT_CMD:
|
|
|
|
base_cmd = "%(timeout_cmd)s -s 9 %(timeout)s %(puppet_cmd)s" % dict(
|
|
|
|
timeout_cmd=TIMEOUT_CMD,
|
|
|
|
timeout=pipes.quote(p['timeout']),
|
|
|
|
puppet_cmd=PUPPET_CMD)
|
|
|
|
else:
|
|
|
|
base_cmd = PUPPET_CMD
|
2015-05-28 22:23:27 +02:00
|
|
|
|
2015-06-06 10:08:16 +02:00
|
|
|
if not p['manifest']:
|
2015-05-29 13:06:15 +02:00
|
|
|
cmd = ("%(base_cmd)s agent --onetime"
|
2015-11-25 13:44:32 +01:00
|
|
|
" --ignorecache --no-daemonize --no-usecacheonfailure"
|
|
|
|
" --no-splay --detailed-exitcodes --verbose") % dict(
|
2015-05-28 22:23:27 +02:00
|
|
|
base_cmd=base_cmd,
|
2015-06-06 10:08:16 +02:00
|
|
|
)
|
|
|
|
if p['puppetmaster']:
|
2015-08-10 19:25:13 +02:00
|
|
|
cmd += " --server %s" % pipes.quote(p['puppetmaster'])
|
2015-05-28 22:23:27 +02:00
|
|
|
if p['show_diff']:
|
2016-01-28 16:48:18 +01:00
|
|
|
cmd += " --show_diff"
|
2015-06-06 09:42:56 +02:00
|
|
|
if p['environment']:
|
|
|
|
cmd += " --environment '%s'" % p['environment']
|
2015-05-29 13:06:15 +02:00
|
|
|
if module.check_mode:
|
|
|
|
cmd += " --noop"
|
2015-06-06 09:13:11 +02:00
|
|
|
else:
|
|
|
|
cmd += " --no-noop"
|
2015-05-28 22:23:27 +02:00
|
|
|
else:
|
2015-05-29 13:06:15 +02:00
|
|
|
cmd = "%s apply --detailed-exitcodes " % base_cmd
|
2015-06-06 09:42:56 +02:00
|
|
|
if p['environment']:
|
|
|
|
cmd += "--environment '%s' " % p['environment']
|
2015-05-29 13:06:15 +02:00
|
|
|
if module.check_mode:
|
|
|
|
cmd += "--noop "
|
2015-06-06 09:13:11 +02:00
|
|
|
else:
|
|
|
|
cmd += "--no-noop "
|
2015-05-29 13:06:15 +02:00
|
|
|
cmd += pipes.quote(p['manifest'])
|
2015-05-28 22:23:27 +02:00
|
|
|
rc, stdout, stderr = module.run_command(cmd)
|
|
|
|
|
|
|
|
if rc == 0:
|
|
|
|
# success
|
|
|
|
module.exit_json(rc=rc, changed=False, stdout=stdout)
|
|
|
|
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
|
2015-11-18 02:33:25 +01:00
|
|
|
module.exit_json(rc=0, changed=True, stdout=stdout, stderr=stderr)
|
2015-05-28 22:23:27 +02:00
|
|
|
elif rc == 124:
|
|
|
|
# timeout
|
|
|
|
module.exit_json(
|
|
|
|
rc=rc, msg="%s timed out" % cmd, stdout=stdout, stderr=stderr)
|
|
|
|
else:
|
|
|
|
# failure
|
|
|
|
module.fail_json(
|
|
|
|
rc=rc, msg="%s failed with return code: %d" % (cmd, rc),
|
|
|
|
stdout=stdout, stderr=stderr)
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
|
|
|
|
main()
|