2013-02-09 17:55:23 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, Chatham Financial <oss@chathamfinancial.com>
|
|
|
|
#
|
|
|
|
# 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: rabbitmq_plugin
|
2014-02-28 18:38:11 +01:00
|
|
|
short_description: Adds or removes plugins to RabbitMQ
|
2013-02-09 17:55:23 +01:00
|
|
|
description:
|
|
|
|
- Enables or disables RabbitMQ plugins
|
2013-03-30 20:44:34 +01:00
|
|
|
version_added: "1.1"
|
2013-02-09 17:55:23 +01:00
|
|
|
author: Chris Hoffman
|
|
|
|
options:
|
|
|
|
names:
|
|
|
|
description:
|
|
|
|
- Comma-separated list of plugin names
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: [name]
|
|
|
|
new_only:
|
|
|
|
description:
|
|
|
|
- Only enable missing plugins
|
|
|
|
- Does not disable plugins that are not in the names list
|
|
|
|
required: false
|
2013-03-12 13:18:12 +01:00
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
2013-02-09 17:55:23 +01:00
|
|
|
state:
|
|
|
|
description:
|
2013-07-09 18:33:30 +02:00
|
|
|
- Specify if plugins are to be enabled or disabled
|
2013-02-09 17:55:23 +01:00
|
|
|
required: false
|
|
|
|
default: enabled
|
|
|
|
choices: [enabled, disabled]
|
2013-07-12 11:13:53 +02:00
|
|
|
prefix:
|
2013-07-09 18:33:14 +02:00
|
|
|
description:
|
2013-07-20 18:08:47 +02:00
|
|
|
- Specify a custom install prefix to a Rabbit
|
2013-07-09 18:33:14 +02:00
|
|
|
required: false
|
2013-07-20 18:08:47 +02:00
|
|
|
version_added: "1.3"
|
2013-07-09 18:33:14 +02:00
|
|
|
default: null
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Enables the rabbitmq_management plugin
|
|
|
|
- rabbitmq_plugin: names=rabbitmq_management state=enabled
|
2013-02-09 17:55:23 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
class RabbitMqPlugins(object):
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
2013-07-09 18:33:14 +02:00
|
|
|
|
2013-07-12 11:13:53 +02:00
|
|
|
if module.params['prefix']:
|
|
|
|
self._rabbitmq_plugins = module.params['prefix'] + "/sbin/rabbitmq-plugins"
|
2013-07-09 18:33:14 +02:00
|
|
|
else:
|
|
|
|
self._rabbitmq_plugins = module.get_bin_path('rabbitmq-plugins', True)
|
2013-02-09 17:55:23 +01:00
|
|
|
|
2013-02-10 23:14:24 +01:00
|
|
|
def _exec(self, args, run_in_check_mode=False):
|
2013-02-10 23:20:44 +01:00
|
|
|
if not self.module.check_mode or (self.module.check_mode and run_in_check_mode):
|
|
|
|
cmd = [self._rabbitmq_plugins]
|
|
|
|
rc, out, err = self.module.run_command(cmd + args, check_rc=True)
|
|
|
|
return out.splitlines()
|
|
|
|
return list()
|
2013-02-09 17:55:23 +01:00
|
|
|
|
|
|
|
def get_all(self):
|
2013-02-10 23:14:24 +01:00
|
|
|
return self._exec(['list', '-E', '-m'], True)
|
2013-02-09 17:55:23 +01:00
|
|
|
|
|
|
|
def enable(self, name):
|
2013-02-10 23:14:24 +01:00
|
|
|
self._exec(['enable', name])
|
2013-02-09 17:55:23 +01:00
|
|
|
|
|
|
|
def disable(self, name):
|
2013-02-10 23:14:24 +01:00
|
|
|
self._exec(['disable', name])
|
2013-02-09 17:55:23 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
arg_spec = dict(
|
|
|
|
names=dict(required=True, aliases=['name']),
|
2013-02-23 22:56:45 +01:00
|
|
|
new_only=dict(default='no', type='bool'),
|
2013-07-09 18:33:14 +02:00
|
|
|
state=dict(default='enabled', choices=['enabled', 'disabled']),
|
2013-07-12 11:13:53 +02:00
|
|
|
prefix=dict(required=False, default=None)
|
2013-02-09 17:55:23 +01:00
|
|
|
)
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=arg_spec,
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
names = module.params['names'].split(',')
|
2013-02-23 19:59:52 +01:00
|
|
|
new_only = module.params['new_only']
|
2013-02-09 17:55:23 +01:00
|
|
|
state = module.params['state']
|
|
|
|
|
|
|
|
rabbitmq_plugins = RabbitMqPlugins(module)
|
|
|
|
enabled_plugins = rabbitmq_plugins.get_all()
|
|
|
|
|
|
|
|
enabled = []
|
|
|
|
disabled = []
|
|
|
|
if state == 'enabled':
|
|
|
|
if not new_only:
|
|
|
|
for plugin in enabled_plugins:
|
|
|
|
if plugin not in names:
|
|
|
|
rabbitmq_plugins.disable(plugin)
|
|
|
|
disabled.append(plugin)
|
|
|
|
|
|
|
|
for name in names:
|
|
|
|
if name not in enabled_plugins:
|
|
|
|
rabbitmq_plugins.enable(name)
|
|
|
|
enabled.append(name)
|
|
|
|
else:
|
|
|
|
for plugin in enabled_plugins:
|
|
|
|
if plugin in names:
|
|
|
|
rabbitmq_plugins.disable(plugin)
|
|
|
|
disabled.append(plugin)
|
|
|
|
|
|
|
|
changed = len(enabled) > 0 or len(disabled) > 0
|
|
|
|
module.exit_json(changed=changed, enabled=enabled, disabled=disabled)
|
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2013-02-09 17:55:23 +01:00
|
|
|
main()
|