Debian Codegraph @

ansible_collections/community/rabbitmq/plugins/modules/rabbitmq_plugin.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2013, Chatham Financial <[email protected]>
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = r'''
12 ---
13 module: rabbitmq_plugin
14 short_description: Manage RabbitMQ plugins
15 description:
16 - This module can be used to enable or disable RabbitMQ plugins.
17 author:
18 - Chris Hoffman (@chrishoffman)
19 options:
20 names:
21 description:
22 - Comma-separated list of plugin names. Also, accepts plugin name.
23 type: str
24 required: true
25 aliases: [name]
26 new_only:
27 description:
28 - Only enable missing plugins.
29 - Does not disable plugins that are not in the names list.
30 type: bool
31 default: false
32 state:
33 description:
34 - Specify if plugins are to be enabled or disabled.
35 type: str
36 default: enabled
37 choices: [enabled, disabled]
38 broker_state:
39 description:
40 - Specify whether the broker should be online or offline for the plugin change.
41 type: str
42 default: online
43 choices: [online, offline]
44 prefix:
45 description:
46 - Specify a custom install prefix to a Rabbit.
47 type: str
48 '''
49
50 EXAMPLES = r'''
51 - name: Enables the rabbitmq_management plugin
52 community.rabbitmq.rabbitmq_plugin:
53 names: rabbitmq_management
54 state: enabled
55
56 - name: Enable multiple rabbitmq plugins
57 community.rabbitmq.rabbitmq_plugin:
58 names: rabbitmq_management,rabbitmq_management_visualiser
59 state: enabled
60
61 - name: Disable plugin
62 community.rabbitmq.rabbitmq_plugin:
63 names: rabbitmq_management
64 state: disabled
65
66 - name: Enable every plugin in list with existing plugins
67 community.rabbitmq.rabbitmq_plugin:
68 names: rabbitmq_management,rabbitmq_management_visualiser,rabbitmq_shovel,rabbitmq_shovel_management
69 state: enabled
70 new_only: true
71
72 - name: Enables the rabbitmq_peer_discovery_aws plugin without requiring a broker connection.
73 community.rabbitmq.rabbitmq_plugin:
74 names: rabbitmq_peer_discovery_aws_plugin
75 state: enabled
76 broker_state: offline
77 '''
78
79 RETURN = r'''
80 enabled:
81 description: list of plugins enabled during task run
82 returned: always
83 type: list
84 sample: ["rabbitmq_management"]
85 disabled:
86 description: list of plugins disabled during task run
87 returned: always
88 type: list
89 sample: ["rabbitmq_management"]
90 '''
91
92 import os
93 from ansible.module_utils.basic import AnsibleModule
94
95
96 class RabbitMqPlugins(object):
97
98 def __init__(self, module):
99 self.module = module
100 bin_path = ''
101 if module.params['prefix']:
102 if os.path.isdir(os.path.join(module.params['prefix'], 'bin')):
103 bin_path = os.path.join(module.params['prefix'], 'bin')
104 elif os.path.isdir(os.path.join(module.params['prefix'], 'sbin')):
105 bin_path = os.path.join(module.params['prefix'], 'sbin')
106 else:
107 # No such path exists.
108 module.fail_json(msg="No binary folder in prefix %s" % module.params['prefix'])
109
110 self._rabbitmq_plugins = os.path.join(bin_path, "rabbitmq-plugins")
111 else:
112 self._rabbitmq_plugins = module.get_bin_path('rabbitmq-plugins', True)
113
114 def _exec(self, args, force_exec_in_check_mode=False):
115 if not self.module.check_mode or (self.module.check_mode and force_exec_in_check_mode):
116 cmd = [self._rabbitmq_plugins]
117 rc, out, err = self.module.run_command(cmd + args, check_rc=True)
118 return out.splitlines()
119 return list()
120
121 def get_all(self):
122 list_output = self._exec(['list', '-E', '-m'], True)
123 plugins = []
124 for plugin in list_output:
125 if not plugin:
126 break
127 plugins.append(plugin)
128
129 return plugins
130
131 def enable(self, name):
132 self._exec(['enable', "--%s" % self.module.params['broker_state'], name])
133
134 def disable(self, name):
135 self._exec(['disable', "--%s" % self.module.params['broker_state'], name])
136
137
138 def main():
139 arg_spec = dict(
140 names=dict(required=True, aliases=['name']),
141 new_only=dict(default='no', type='bool'),
142 state=dict(default='enabled', choices=['enabled', 'disabled']),
143 broker_state=dict(default='online', choices=['online', 'offline']),
144 prefix=dict(required=False, default=None)
145 )
146 module = AnsibleModule(
147 argument_spec=arg_spec,
148 supports_check_mode=True
149 )
150
151 result = dict()
152 names = module.params['names'].split(',')
153 new_only = module.params['new_only']
154 state = module.params['state']
155
156 rabbitmq_plugins = RabbitMqPlugins(module)
157 enabled_plugins = rabbitmq_plugins.get_all()
158
159 enabled = []
160 disabled = []
161 if state == 'enabled':
162 if not new_only:
163 for plugin in enabled_plugins:
164 if " " in plugin:
165 continue
166 if plugin not in names:
167 rabbitmq_plugins.disable(plugin)
168 disabled.append(plugin)
169
170 for name in names:
171 if name not in enabled_plugins:
172 rabbitmq_plugins.enable(name)
173 enabled.append(name)
174 else:
175 for plugin in enabled_plugins:
176 if plugin in names:
177 rabbitmq_plugins.disable(plugin)
178 disabled.append(plugin)
179
180 result['changed'] = len(enabled) > 0 or len(disabled) > 0
181 result['enabled'] = enabled
182 result['disabled'] = disabled
183 module.exit_json(**result)
184
185
186 if __name__ == '__main__':
187 main()