1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2018, Hiroyuki Matsuo <h.matsuo.engineer@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = r'''
---
module: rabbitmq_vhost_limits
author: Hiroyuki Matsuo (@h-matsuo)
short_description: Manage the state of virtual host limits in RabbitMQ
description:
- This module sets/clears certain limits on a virtual host.
- The configurable limits are I(max_connections) and I(max-queues).
options:
max_connections:
description:
- Max number of concurrent client connections.
- Negative value means "no limit".
- Ignored when the I(state) is C(absent).
type: int
default: -1
max_queues:
description:
- Max number of queues.
- Negative value means "no limit".
- Ignored when the I(state) is C(absent).
type: int
default: -1
node:
description:
- Name of the RabbitMQ Erlang node to manage.
type: str
state:
description:
- Specify whether the limits are to be set or cleared.
- If set to C(absent), the limits of both I(max_connections) and I(max-queues) will be cleared.
type: str
default: present
choices: [present, absent]
vhost:
description:
- Name of the virtual host to manage.
type: str
default: /
'''
EXAMPLES = r'''
- name: Limit both of the max number of connections and queues on the vhost '/'.
community.rabbitmq.rabbitmq_vhost_limits:
vhost: /
max_connections: 64
max_queues: 256
state: present
- name: |-
Limit the max number of connections on the vhost '/'.
This task implicitly clears the max number of queues limit using default value: -1.
community.rabbitmq.rabbitmq_vhost_limits:
vhost: /
max_connections: 64
state: present
- name: Clear the limits on the vhost '/'.
community.rabbitmq.rabbitmq_vhost_limits:
vhost: /
state: absent
'''
RETURN = r''' # '''
import json
from ansible.module_utils.basic import AnsibleModule
class RabbitMqVhostLimits(object):
def __init__(self, module):
self._module = module
self._max_connections = module.params['max_connections']
self._max_queues = module.params['max_queues']
self._node = module.params['node']
self._state = module.params['state']
self._vhost = module.params['vhost']
self._rabbitmqctl = module.get_bin_path('rabbitmqctl', True)
def _exec(self, args):
cmd = [self._rabbitmqctl, '-q', '-p', self._vhost]
if self._node is not None:
cmd.extend(['-n', self._node])
rc, out, err = self._module.run_command(cmd + args, check_rc=True)
return dict(rc=rc, out=out.splitlines(), err=err.splitlines())
def list(self):
exec_result = self._exec(['list_vhost_limits'])
vhost_limits = exec_result['out'][0]
max_connections = None
max_queues = None
if vhost_limits:
vhost_limits = json.loads(vhost_limits)
if 'max-connections' in vhost_limits:
max_connections = vhost_limits['max-connections']
if 'max-queues' in vhost_limits:
max_queues = vhost_limits['max-queues']
return dict(
max_connections=max_connections,
max_queues=max_queues
)
def set(self):
if self._module.check_mode:
return
json_str = '{{"max-connections": {0}, "max-queues": {1}}}'.format(self._max_connections, self._max_queues)
self._exec(['set_vhost_limits', json_str])
def clear(self):
if self._module.check_mode:
return
self._exec(['clear_vhost_limits'])
def main():
arg_spec = dict(
max_connections=dict(default=-1, type='int'),
max_queues=dict(default=-1, type='int'),
node=dict(default=None, type='str'),
state=dict(default='present', choices=['present', 'absent'], type='str'),
vhost=dict(default='/', type='str')
)
module = AnsibleModule(
argument_spec=arg_spec,
supports_check_mode=True
)
max_connections = module.params['max_connections']
max_queues = module.params['max_queues']
node = module.params['node']
state = module.params['state']
vhost = module.params['vhost']
module_result = dict(changed=False)
rabbitmq_vhost_limits = RabbitMqVhostLimits(module)
current_status = rabbitmq_vhost_limits.list()
if state == 'present':
wanted_status = dict(
max_connections=max_connections,
max_queues=max_queues
)
else: # state == 'absent'
wanted_status = dict(
max_connections=None,
max_queues=None
)
if current_status != wanted_status:
module_result['changed'] = True
if state == 'present':
rabbitmq_vhost_limits.set()
else: # state == 'absent'
rabbitmq_vhost_limits.clear()
module.exit_json(**module_result)
if __name__ == '__main__':
main()
|