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_user
|
|
|
|
short_description: Adds or removes users to RabbitMQ
|
|
|
|
description:
|
|
|
|
- Add or remove users to RabbitMQ and assign permissions
|
2013-03-30 20:44:34 +01:00
|
|
|
version_added: "1.1"
|
2015-05-14 16:45:21 +02:00
|
|
|
author: '"Chris Hoffman (@chrishoffman)"'
|
2013-02-09 17:55:23 +01:00
|
|
|
options:
|
|
|
|
user:
|
|
|
|
description:
|
|
|
|
- Name of user to add
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: [username, name]
|
|
|
|
password:
|
|
|
|
description:
|
2013-06-23 17:05:09 +02:00
|
|
|
- Password of user to add.
|
|
|
|
- To change the password of an existing user, you must also specify
|
|
|
|
C(force=yes).
|
2013-02-09 17:55:23 +01:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
tags:
|
|
|
|
description:
|
|
|
|
- User tags specified as comma delimited
|
|
|
|
required: false
|
|
|
|
default: null
|
2015-08-13 19:48:19 +02:00
|
|
|
permissions:
|
|
|
|
description:
|
|
|
|
- a list of dicts, each dict contains vhost, configure_priv, write_priv, and read_priv,
|
|
|
|
and represents a permission rule for that vhost.
|
|
|
|
- This option should be preferable when you care about all permissions of the user.
|
|
|
|
- You should use vhost, configure_priv, write_priv, and read_priv options instead
|
|
|
|
if you care about permissions for just some vhosts.
|
|
|
|
required: false
|
|
|
|
default: []
|
2013-02-09 17:55:23 +01:00
|
|
|
vhost:
|
|
|
|
description:
|
|
|
|
- vhost to apply access privileges.
|
2015-08-13 19:48:19 +02:00
|
|
|
- This option will be ignored when permissions option is used.
|
2013-02-09 17:55:23 +01:00
|
|
|
required: false
|
|
|
|
default: /
|
2013-05-07 18:20:26 +02:00
|
|
|
node:
|
|
|
|
description:
|
|
|
|
- erlang node name of the rabbit we wish to configure
|
|
|
|
required: false
|
|
|
|
default: rabbit
|
2014-04-08 16:40:41 +02:00
|
|
|
version_added: "1.2"
|
2013-02-09 17:55:23 +01:00
|
|
|
configure_priv:
|
|
|
|
description:
|
|
|
|
- Regular expression to restrict configure actions on a resource
|
|
|
|
for the specified vhost.
|
|
|
|
- By default all actions are restricted.
|
2015-08-13 19:48:19 +02:00
|
|
|
- This option will be ignored when permissions option is used.
|
2013-02-09 17:55:23 +01:00
|
|
|
required: false
|
|
|
|
default: ^$
|
|
|
|
write_priv:
|
|
|
|
description:
|
|
|
|
- Regular expression to restrict configure actions on a resource
|
|
|
|
for the specified vhost.
|
|
|
|
- By default all actions are restricted.
|
2015-08-13 19:48:19 +02:00
|
|
|
- This option will be ignored when permissions option is used.
|
2013-02-09 17:55:23 +01:00
|
|
|
required: false
|
|
|
|
default: ^$
|
|
|
|
read_priv:
|
|
|
|
description:
|
|
|
|
- Regular expression to restrict configure actions on a resource
|
|
|
|
for the specified vhost.
|
|
|
|
- By default all actions are restricted.
|
2015-08-13 19:48:19 +02:00
|
|
|
- This option will be ignored when permissions option is used.
|
2013-02-09 17:55:23 +01:00
|
|
|
required: false
|
|
|
|
default: ^$
|
|
|
|
force:
|
|
|
|
description:
|
|
|
|
- Deletes and recreates the user.
|
|
|
|
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:
|
|
|
|
- Specify if user is to be added or removed
|
|
|
|
required: false
|
|
|
|
default: present
|
|
|
|
choices: [present, absent]
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2015-08-13 19:48:19 +02:00
|
|
|
# Add user to server and assign full access control on / vhost.
|
|
|
|
# The user might have permission rules for other vhost but you don't care.
|
2013-06-14 11:53:43 +02:00
|
|
|
- rabbitmq_user: user=joe
|
|
|
|
password=changeme
|
|
|
|
vhost=/
|
|
|
|
configure_priv=.*
|
|
|
|
read_priv=.*
|
|
|
|
write_priv=.*
|
|
|
|
state=present
|
2015-08-13 19:48:19 +02:00
|
|
|
|
|
|
|
# Add user to server and assign full access control on / vhost.
|
|
|
|
# The user doesn't have permission rules for other vhosts
|
|
|
|
- rabbitmq_user: user=joe
|
|
|
|
password=changeme
|
|
|
|
permissions=[{vhost='/', configure_priv='.*', read_priv='.*', write_priv='.*'}]
|
|
|
|
state=present
|
2013-02-09 17:55:23 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
class RabbitMqUser(object):
|
2015-08-13 19:48:19 +02:00
|
|
|
def __init__(self, module, username, password, tags, permissions,
|
|
|
|
node, bulk_permissions=False):
|
2013-02-09 17:55:23 +01:00
|
|
|
self.module = module
|
|
|
|
self.username = username
|
|
|
|
self.password = password
|
2013-05-08 16:34:51 +02:00
|
|
|
self.node = node
|
2015-08-24 18:54:44 +02:00
|
|
|
if not tags:
|
2013-02-10 23:00:09 +01:00
|
|
|
self.tags = list()
|
2013-02-09 17:55:23 +01:00
|
|
|
else:
|
|
|
|
self.tags = tags.split(',')
|
|
|
|
|
|
|
|
self.permissions = permissions
|
2015-08-13 19:48:19 +02:00
|
|
|
self.bulk_permissions = bulk_permissions
|
2013-02-09 17:55:23 +01:00
|
|
|
|
|
|
|
self._tags = None
|
|
|
|
self._permissions = None
|
2013-02-10 23:02:50 +01:00
|
|
|
self._rabbitmqctl = module.get_bin_path('rabbitmqctl', True)
|
2013-02-09 17:55:23 +01:00
|
|
|
|
2013-02-10 23:20:44 +01:00
|
|
|
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:20:44 +01:00
|
|
|
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(self):
|
2013-02-10 23:20:44 +01:00
|
|
|
users = self._exec(['list_users'], True)
|
2013-02-09 17:55:23 +01:00
|
|
|
|
|
|
|
for user_tag in users:
|
2015-09-05 18:37:32 +02:00
|
|
|
if '\t' not in user_tag:
|
|
|
|
continue
|
|
|
|
|
2013-02-09 17:55:23 +01:00
|
|
|
user, tags = user_tag.split('\t')
|
|
|
|
|
|
|
|
if user == self.username:
|
|
|
|
for c in ['[',']',' ']:
|
|
|
|
tags = tags.replace(c, '')
|
|
|
|
|
|
|
|
if tags != '':
|
|
|
|
self._tags = tags.split(',')
|
|
|
|
else:
|
2013-02-10 23:00:09 +01:00
|
|
|
self._tags = list()
|
2013-02-09 17:55:23 +01:00
|
|
|
|
|
|
|
self._permissions = self._get_permissions()
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _get_permissions(self):
|
2013-02-10 23:20:44 +01:00
|
|
|
perms_out = self._exec(['list_user_permissions', self.username], True)
|
2013-02-09 17:55:23 +01:00
|
|
|
|
2015-08-13 19:48:19 +02:00
|
|
|
perms_list = list()
|
2013-02-09 17:55:23 +01:00
|
|
|
for perm in perms_out:
|
|
|
|
vhost, configure_priv, write_priv, read_priv = perm.split('\t')
|
2015-08-13 19:48:19 +02:00
|
|
|
if not self.bulk_permissions:
|
|
|
|
if vhost == self.permissions[0]['vhost']:
|
|
|
|
perms_list.append(dict(vhost=vhost, configure_priv=configure_priv,
|
|
|
|
write_priv=write_priv, read_priv=read_priv))
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
perms_list.append(dict(vhost=vhost, configure_priv=configure_priv,
|
|
|
|
write_priv=write_priv, read_priv=read_priv))
|
|
|
|
return perms_list
|
2013-02-09 17:55:23 +01:00
|
|
|
|
|
|
|
def add(self):
|
2014-10-24 13:41:29 +02:00
|
|
|
if self.password is not None:
|
|
|
|
self._exec(['add_user', self.username, self.password])
|
2015-01-19 18:40:04 +01:00
|
|
|
else:
|
2014-10-24 13:41:29 +02:00
|
|
|
self._exec(['add_user', self.username, ''])
|
|
|
|
self._exec(['clear_password', self.username])
|
2013-02-09 17:55:23 +01:00
|
|
|
|
|
|
|
def delete(self):
|
2013-02-10 23:20:44 +01:00
|
|
|
self._exec(['delete_user', self.username])
|
2013-02-09 17:55:23 +01:00
|
|
|
|
|
|
|
def set_tags(self):
|
2013-02-10 23:20:44 +01:00
|
|
|
self._exec(['set_user_tags', self.username] + self.tags)
|
2013-02-09 17:55:23 +01:00
|
|
|
|
|
|
|
def set_permissions(self):
|
2015-08-13 19:48:19 +02:00
|
|
|
for permission in self._permissions:
|
|
|
|
if permission not in self.permissions:
|
|
|
|
cmd = ['clear_permissions', '-p']
|
|
|
|
cmd.append(permission['vhost'])
|
|
|
|
cmd.append(self.username)
|
|
|
|
self._exec(cmd)
|
|
|
|
for permission in self.permissions:
|
|
|
|
if permission not in self._permissions:
|
|
|
|
cmd = ['set_permissions', '-p']
|
|
|
|
cmd.append(permission['vhost'])
|
|
|
|
cmd.append(self.username)
|
|
|
|
cmd.append(permission['configure_priv'])
|
|
|
|
cmd.append(permission['write_priv'])
|
|
|
|
cmd.append(permission['read_priv'])
|
|
|
|
self._exec(cmd)
|
2013-02-09 17:55:23 +01:00
|
|
|
|
|
|
|
def has_tags_modifications(self):
|
2013-02-10 20:37:21 +01:00
|
|
|
return set(self.tags) != set(self._tags)
|
2013-02-09 17:55:23 +01:00
|
|
|
|
|
|
|
def has_permissions_modifications(self):
|
|
|
|
return self._permissions != self.permissions
|
|
|
|
|
|
|
|
def main():
|
|
|
|
arg_spec = dict(
|
|
|
|
user=dict(required=True, aliases=['username', 'name']),
|
|
|
|
password=dict(default=None),
|
|
|
|
tags=dict(default=None),
|
2015-08-13 19:48:19 +02:00
|
|
|
permissions=dict(default=list()),
|
2013-02-09 17:55:23 +01:00
|
|
|
vhost=dict(default='/'),
|
|
|
|
configure_priv=dict(default='^$'),
|
|
|
|
write_priv=dict(default='^$'),
|
|
|
|
read_priv=dict(default='^$'),
|
2013-02-23 22:56:45 +01:00
|
|
|
force=dict(default='no', type='bool'),
|
2013-05-07 18:20:26 +02:00
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
|
|
|
node=dict(default='rabbit')
|
2013-02-09 17:55:23 +01:00
|
|
|
)
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=arg_spec,
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
username = module.params['user']
|
|
|
|
password = module.params['password']
|
|
|
|
tags = module.params['tags']
|
2015-08-13 19:48:19 +02:00
|
|
|
permissions = module.params['permissions']
|
2013-02-09 17:55:23 +01:00
|
|
|
vhost = module.params['vhost']
|
|
|
|
configure_priv = module.params['configure_priv']
|
|
|
|
write_priv = module.params['write_priv']
|
|
|
|
read_priv = module.params['read_priv']
|
2013-02-23 19:59:52 +01:00
|
|
|
force = module.params['force']
|
2013-02-09 17:55:23 +01:00
|
|
|
state = module.params['state']
|
2013-05-07 18:20:26 +02:00
|
|
|
node = module.params['node']
|
2013-02-09 17:55:23 +01:00
|
|
|
|
2015-08-13 19:48:19 +02:00
|
|
|
bulk_permissions = True
|
|
|
|
if permissions == []:
|
|
|
|
perm = {
|
|
|
|
'vhost': vhost,
|
|
|
|
'configure_priv': configure_priv,
|
|
|
|
'write_priv': write_priv,
|
|
|
|
'read_priv': read_priv
|
|
|
|
}
|
|
|
|
permissions.append(perm)
|
|
|
|
bulk_permissions = False
|
|
|
|
|
|
|
|
rabbitmq_user = RabbitMqUser(module, username, password, tags, permissions,
|
|
|
|
node, bulk_permissions=bulk_permissions)
|
2013-02-09 17:55:23 +01:00
|
|
|
|
|
|
|
changed = False
|
|
|
|
if rabbitmq_user.get():
|
|
|
|
if state == 'absent':
|
|
|
|
rabbitmq_user.delete()
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
if force:
|
|
|
|
rabbitmq_user.delete()
|
|
|
|
rabbitmq_user.add()
|
|
|
|
rabbitmq_user.get()
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
if rabbitmq_user.has_tags_modifications():
|
|
|
|
rabbitmq_user.set_tags()
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
if rabbitmq_user.has_permissions_modifications():
|
|
|
|
rabbitmq_user.set_permissions()
|
|
|
|
changed = True
|
|
|
|
elif state == 'present':
|
|
|
|
rabbitmq_user.add()
|
|
|
|
rabbitmq_user.set_tags()
|
|
|
|
rabbitmq_user.set_permissions()
|
|
|
|
changed = True
|
|
|
|
|
2013-02-10 20:37:21 +01:00
|
|
|
module.exit_json(changed=changed, user=username, state=state)
|
2013-02-09 17:55:23 +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 17:55:23 +01:00
|
|
|
main()
|