2013-02-09 19:28:41 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-03-01 02:32:44 +01:00
|
|
|
# (c) 2013, Chatham Financial <oss@chathamfinancial.com>
|
2013-02-09 19:28:41 +01:00
|
|
|
#
|
|
|
|
# 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_vhost
|
|
|
|
short_description: Manage the state of a virtual host in RabbitMQ
|
|
|
|
description:
|
|
|
|
- Manage the state of a virtual host in RabbitMQ
|
2013-03-30 20:44:34 +01:00
|
|
|
version_added: "1.1"
|
|
|
|
author: Chris Hoffman
|
2013-02-09 19:28:41 +01:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The name of the vhost to manage
|
|
|
|
required: true
|
|
|
|
default: null
|
2013-02-10 23:08:33 +01:00
|
|
|
aliases: [vhost]
|
2013-05-07 18:20:26 +02:00
|
|
|
node:
|
|
|
|
description:
|
|
|
|
- erlang node name of the rabbit we wish to configure
|
|
|
|
required: false
|
|
|
|
default: rabbit
|
2013-02-10 23:01:20 +01:00
|
|
|
tracing:
|
|
|
|
description:
|
2013-03-30 20:53:28 +01:00
|
|
|
- Enable/disable tracing for a vhost
|
2013-03-12 13:18:12 +01:00
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
2013-02-10 23:08:33 +01:00
|
|
|
aliases: [trace]
|
2013-02-09 19:28:41 +01:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- The state of vhost
|
2013-02-10 23:01:20 +01:00
|
|
|
default: present
|
|
|
|
choices: [present, absent]
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Ensure that the vhost /test exists.
|
|
|
|
- rabbitmq_vhost: name=/test state=present
|
2013-02-09 19:28:41 +01:00
|
|
|
'''
|
|
|
|
|
2013-02-10 23:01:20 +01:00
|
|
|
class RabbitMqVhost(object):
|
2013-05-07 18:20:26 +02:00
|
|
|
def __init__(self, module, name, tracing, node):
|
2013-02-10 23:01:20 +01:00
|
|
|
self.module = module
|
|
|
|
self.name = name
|
|
|
|
self.tracing = tracing
|
2013-05-08 16:34:51 +02:00
|
|
|
self.node = node
|
2013-02-10 23:01:20 +01:00
|
|
|
|
|
|
|
self._tracing = False
|
|
|
|
self._rabbitmqctl = module.get_bin_path('rabbitmqctl', True)
|
|
|
|
|
|
|
|
def _exec(self, args, run_in_check_mode=False):
|
|
|
|
if not self.module.check_mode or (self.module.check_mode and run_in_check_mode):
|
2013-05-26 15:51:50 +02:00
|
|
|
cmd = [self._rabbitmqctl, '-q', '-n', self.node]
|
2013-02-10 23:01:20 +01:00
|
|
|
rc, out, err = self.module.run_command(cmd + args, check_rc=True)
|
|
|
|
return out.splitlines()
|
|
|
|
return list()
|
|
|
|
|
|
|
|
def get(self):
|
|
|
|
vhosts = self._exec(['list_vhosts', 'name', 'tracing'], True)
|
|
|
|
|
|
|
|
for vhost in vhosts:
|
|
|
|
name, tracing = vhost.split('\t')
|
|
|
|
if name == self.name:
|
|
|
|
self._tracing = self.module.boolean(tracing)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def add(self):
|
|
|
|
return self._exec(['add_vhost', self.name])
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
return self._exec(['delete_vhost', self.name])
|
|
|
|
|
|
|
|
def set_tracing(self):
|
|
|
|
if self.tracing != self._tracing:
|
|
|
|
if self.tracing:
|
|
|
|
self._enable_tracing()
|
|
|
|
else:
|
|
|
|
self._disable_tracing()
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _enable_tracing(self):
|
|
|
|
return self._exec(['trace_on', '-p', self.name])
|
|
|
|
|
|
|
|
def _disable_tracing(self):
|
|
|
|
return self._exec(['trace_off', '-p', self.name])
|
|
|
|
|
2013-02-09 19:28:41 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
arg_spec = dict(
|
2013-02-10 23:08:33 +01:00
|
|
|
name=dict(required=True, aliases=['vhost']),
|
2013-02-23 22:56:45 +01:00
|
|
|
tracing=dict(default='off', aliases=['trace'], type='bool'),
|
2013-05-07 18:20:26 +02:00
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
|
|
|
node=dict(default='rabbit'),
|
2013-02-09 19:28:41 +01:00
|
|
|
)
|
|
|
|
|
2013-02-10 23:01:20 +01:00
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=arg_spec,
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
2013-02-09 19:28:41 +01:00
|
|
|
|
|
|
|
name = module.params['name']
|
2013-02-23 19:59:52 +01:00
|
|
|
tracing = module.params['tracing']
|
2013-02-09 19:28:41 +01:00
|
|
|
state = module.params['state']
|
2013-05-07 18:20:26 +02:00
|
|
|
node = module.params['node']
|
2013-02-09 19:28:41 +01:00
|
|
|
|
2013-05-07 18:20:26 +02:00
|
|
|
rabbitmq_vhost = RabbitMqVhost(module, name, tracing, node)
|
2013-02-09 19:28:41 +01:00
|
|
|
|
2013-02-10 23:01:20 +01:00
|
|
|
changed = False
|
|
|
|
if rabbitmq_vhost.get():
|
|
|
|
if state == 'absent':
|
|
|
|
rabbitmq_vhost.delete()
|
|
|
|
changed = True
|
2013-02-09 19:28:41 +01:00
|
|
|
else:
|
2013-02-10 23:01:20 +01:00
|
|
|
if rabbitmq_vhost.set_tracing():
|
|
|
|
changed = True
|
|
|
|
elif state == 'present':
|
|
|
|
rabbitmq_vhost.add()
|
|
|
|
rabbitmq_vhost.set_tracing()
|
|
|
|
changed = True
|
2013-02-09 19:28:41 +01:00
|
|
|
|
2013-02-10 23:01:20 +01:00
|
|
|
module.exit_json(changed=changed, name=name, state=state)
|
2013-02-09 19:28:41 +01:00
|
|
|
|
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 19:28:41 +01:00
|
|
|
main()
|