rabbitmq_plugin 3.84 KB
Newer Older
Chris Hoffman committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/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
24
short_description: Adds or removes plugins to RabbitMQ
Chris Hoffman committed
25 26
description:
  - Enables or disables RabbitMQ plugins
27
version_added: "1.1"
Chris Hoffman committed
28 29 30 31 32 33 34 35 36 37 38 39 40
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
41 42
    default: "no"
    choices: [ "yes", "no" ]
Chris Hoffman committed
43 44
  state:
    description:
Frank Shearar committed
45
      - Specify if plugins are to be enabled or disabled
Chris Hoffman committed
46 47 48
    required: false
    default: enabled
    choices: [enabled, disabled]
49
  prefix:
50
    description:
51
      - Specify a custom install prefix to a Rabbit
52
    required: false
53
    version_added: "1.3"
54
    default: null
55 56 57 58 59
'''

EXAMPLES = '''
# Enables the rabbitmq_management plugin
- rabbitmq_plugin: names=rabbitmq_management state=enabled
Chris Hoffman committed
60 61 62 63 64
'''

class RabbitMqPlugins(object):
    def __init__(self, module):
        self.module = module
65

66 67
        if module.params['prefix']:
            self._rabbitmq_plugins = module.params['prefix'] + "/sbin/rabbitmq-plugins"
68 69
        else:
            self._rabbitmq_plugins = module.get_bin_path('rabbitmq-plugins', True)
Chris Hoffman committed
70

71
    def _exec(self, args, run_in_check_mode=False):
72 73 74 75 76
        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()
Chris Hoffman committed
77 78

    def get_all(self):
79
        return self._exec(['list', '-E', '-m'], True)
Chris Hoffman committed
80 81

    def enable(self, name):
82
        self._exec(['enable', name])
Chris Hoffman committed
83 84

    def disable(self, name):
85
        self._exec(['disable', name])
Chris Hoffman committed
86 87 88 89

def main():
    arg_spec = dict(
        names=dict(required=True, aliases=['name']),
90
        new_only=dict(default='no', type='bool'),
91
        state=dict(default='enabled', choices=['enabled', 'disabled']),
92
        prefix=dict(required=False, default=None)
Chris Hoffman committed
93 94 95 96 97 98 99
    )
    module = AnsibleModule(
        argument_spec=arg_spec,
        supports_check_mode=True
    )

    names = module.params['names'].split(',')
100
    new_only = module.params['new_only']
Chris Hoffman committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    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)

128
# import module snippets
129
from ansible.module_utils.basic import *
Chris Hoffman committed
130
main()