Created
May 20, 2014 16:20
-
-
Save omgjlk/d2116a7dcd736ad75432 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python -tt | |
# This file is part of Ansible | |
# | |
# Ansible 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. | |
# | |
# Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>. | |
DOCUMENTATION = ''' | |
--- | |
module: puppet | |
short_description: Execute puppet actions | |
description: | |
- Executes puppet actions and properly handles detailed return codes | |
version_added: "1.3" | |
options: | |
action: | |
description: | |
- Puppet action to execute | |
choices: ['apply', 'agent'] | |
execute: | |
description: | |
- Execute a specific piece of Puppet code | |
default: null | |
manifest: | |
description: | |
- Execute a specific manfest file | |
default: null | |
modulepath: | |
description: | |
- Specify an an alternative path to the puppet modules | |
default: null | |
noop: | |
description: | |
- Execute in noop mode | |
default: null | |
state: | |
description: | |
- Set the state of the agent | |
choices: ['enable', 'disable'] | |
default: null | |
logpath: | |
description: | |
- Write output to a log rather than returning it in json | |
default: null | |
author: Jesse Keating | |
''' | |
EXAMPLES = ''' | |
# Apply a specific manifest with a specific module path but | |
# do not actually make any changes | |
- action: | |
module: puppet | |
action: apply | |
noop: true | |
modulepath: /opt/rackstack/current/rackstack/modules | |
manifest: /opt/rackstack/current/rackstack/masterless.pp | |
''' | |
import subprocess | |
import sys | |
import time | |
import os | |
import xmlrpclib | |
# Puppet exit codes | |
ERROR_STATES = (1, 4, 6) | |
CHANGED = 2 | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
action = dict(required=True, | |
choices=['agent', 'apply']), | |
execute = dict(), | |
manifest = dict(), | |
modulepath = dict(), | |
noop = dict(), | |
state = dict(choices=['enable', 'disable']), | |
logpath = dict(), | |
) | |
) | |
action = module.params.get('action') | |
execute = module.params.get('execute') | |
manifest = module.params.get('manifest') | |
modulepath = module.params.get('modulepath') | |
noop = module.params.get('noop') | |
state = module.params.get('state') | |
logpath = module.params.get('logpath') | |
data = {} | |
if action in ('apply', 'agent'): | |
cmd = ['puppet', action, '--detailed-exitcodes', '--color=false'] | |
if action == 'agent': | |
if state: | |
cmd.append('--' + state) | |
if execute: | |
cmd.extend(['-e', execute]) | |
if modulepath: | |
cmd.extend(['--modulepath', modulepath]) | |
if manifest: | |
cmd.append(manifest) | |
if noop: | |
cmd.append('--noop') | |
(rc, out, err) = module.run_command(cmd) | |
data['stdout'] = out | |
data['stderr'] = err | |
if rc in ERROR_STATES: | |
msg = err.rstrip() | |
module.fail_json(cmd=cmd, rc=rc, stdout=out, stderr=err, msg=msg) | |
if rc == CHANGED: | |
data['changed'] = True | |
# Deal with output | |
if logpath: | |
logfile = open(logpath, 'a') | |
logfile.write(out) | |
logfile.write(err) | |
logfile.close() | |
del(data['stdout']) | |
del(data['stderr']) | |
module.exit_json(**data) | |
# this is magic, see lib/ansible/module_common.py | |
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment