2014-09-26 01:01:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-10-30 19:54:38 +00:00
|
|
|
# Copyright: (c) 2013, Chatham Financial <oss@chathamfinancial.com>
|
2017-07-29 07:20:36 +00:00
|
|
|
# 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
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:25 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: rabbitmq_user
|
2018-10-30 19:54:38 +00:00
|
|
|
short_description: Manage RabbitMQ users
|
2014-09-26 01:01:01 +00:00
|
|
|
description:
|
|
|
|
- Add or remove users to RabbitMQ and assign permissions
|
|
|
|
version_added: "1.1"
|
2018-10-30 19:54:38 +00:00
|
|
|
author: Chris Hoffman (@chrishoffman)
|
2014-09-26 01:01:01 +00:00
|
|
|
options:
|
|
|
|
user:
|
|
|
|
description:
|
|
|
|
- Name of user to add
|
|
|
|
required: true
|
|
|
|
aliases: [username, name]
|
|
|
|
password:
|
|
|
|
description:
|
|
|
|
- Password of user to add.
|
|
|
|
- To change the password of an existing user, you must also specify
|
2018-05-23 19:34:46 +00:00
|
|
|
C(update_password=always).
|
2014-09-26 01:01:01 +00:00
|
|
|
tags:
|
|
|
|
description:
|
|
|
|
- User tags specified as comma delimited
|
2015-08-13 17:48:19 +00: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.
|
|
|
|
default: []
|
2014-09-26 01:01:01 +00:00
|
|
|
vhost:
|
|
|
|
description:
|
|
|
|
- vhost to apply access privileges.
|
2015-08-13 17:48:19 +00:00
|
|
|
- This option will be ignored when permissions option is used.
|
2014-09-26 01:01:01 +00:00
|
|
|
default: /
|
|
|
|
node:
|
|
|
|
description:
|
|
|
|
- erlang node name of the rabbit we wish to configure
|
|
|
|
default: rabbit
|
|
|
|
version_added: "1.2"
|
|
|
|
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 17:48:19 +00:00
|
|
|
- This option will be ignored when permissions option is used.
|
2014-09-26 01:01:01 +00:00
|
|
|
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 17:48:19 +00:00
|
|
|
- This option will be ignored when permissions option is used.
|
2014-09-26 01:01:01 +00:00
|
|
|
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 17:48:19 +00:00
|
|
|
- This option will be ignored when permissions option is used.
|
2014-09-26 01:01:01 +00:00
|
|
|
default: ^$
|
|
|
|
force:
|
|
|
|
description:
|
|
|
|
- Deletes and recreates the user.
|
2018-03-15 21:15:24 +00:00
|
|
|
type: bool
|
|
|
|
default: 'no'
|
2014-09-26 01:01:01 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Specify if user is to be added or removed
|
|
|
|
default: present
|
|
|
|
choices: [present, absent]
|
2018-05-23 19:34:46 +00:00
|
|
|
update_password:
|
|
|
|
description:
|
|
|
|
- C(on_create) will only set the password for newly created users. C(always) will update passwords if they differ.
|
|
|
|
required: false
|
|
|
|
default: on_create
|
|
|
|
choices: [ on_create, always ]
|
|
|
|
version_added: "2.6"
|
2014-09-26 01:01:01 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2015-08-13 17:48:19 +00: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.
|
2016-12-01 13:59:53 +00:00
|
|
|
- rabbitmq_user:
|
|
|
|
user: joe
|
|
|
|
password: changeme
|
|
|
|
vhost: /
|
|
|
|
configure_priv: .*
|
|
|
|
read_priv: .*
|
|
|
|
write_priv: .*
|
|
|
|
state: present
|
2015-08-13 17:48:19 +00:00
|
|
|
|
|
|
|
# Add user to server and assign full access control on / vhost.
|
|
|
|
# The user doesn't have permission rules for other vhosts
|
2016-12-01 13:59:53 +00:00
|
|
|
- rabbitmq_user:
|
|
|
|
user: joe
|
|
|
|
password: changeme
|
|
|
|
permissions:
|
|
|
|
- vhost: /
|
|
|
|
configure_priv: .*
|
|
|
|
read_priv: .*
|
|
|
|
write_priv: .*
|
|
|
|
state: present
|
2014-09-26 01:01:01 +00:00
|
|
|
'''
|
|
|
|
|
2017-07-29 07:20:36 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2018-12-07 13:19:08 +00:00
|
|
|
from ansible.module_utils.common.collections import count
|
2017-07-29 07:20:36 +00:00
|
|
|
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
class RabbitMqUser(object):
|
2015-08-13 17:48:19 +00:00
|
|
|
def __init__(self, module, username, password, tags, permissions,
|
|
|
|
node, bulk_permissions=False):
|
2014-09-26 01:01:01 +00:00
|
|
|
self.module = module
|
|
|
|
self.username = username
|
|
|
|
self.password = password
|
|
|
|
self.node = node
|
2015-08-24 16:54:44 +00:00
|
|
|
if not tags:
|
2014-09-26 01:01:01 +00:00
|
|
|
self.tags = list()
|
|
|
|
else:
|
|
|
|
self.tags = tags.split(',')
|
|
|
|
|
|
|
|
self.permissions = permissions
|
2015-08-13 17:48:19 +00:00
|
|
|
self.bulk_permissions = bulk_permissions
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
self._tags = None
|
2016-05-05 12:43:36 +00:00
|
|
|
self._permissions = []
|
2014-09-26 01:01:01 +00:00
|
|
|
self._rabbitmqctl = module.get_bin_path('rabbitmqctl', True)
|
|
|
|
|
|
|
|
def _exec(self, args, run_in_check_mode=False):
|
2017-03-03 19:32:37 +00:00
|
|
|
if not self.module.check_mode or run_in_check_mode:
|
2016-04-14 21:44:28 +00:00
|
|
|
cmd = [self._rabbitmqctl, '-q']
|
rabbitmq_user: 'node' parameter: add default value (#38156)
* Remove unnecessary workaround
* add test: set RABBITMQ_NODENAME environment variable
The following error occurs:
TASK [rabbitmq_user : Add user] ***
fatal: [testhost]: FAILED! => {
"changed": false,
"cmd": "/usr/sbin/rabbitmqctl -q list_users",
"rc": 69,
"msg": "Error:********@c65c2bc59398'. Please see diagnostics information and suggestions below.\n\nMost common reasons for this are:\n\n * Target node is unreachable (e.g. due to hostname resolution, TCP connection or firewall issues)\n * CLI tool fails to authenticate with the server (e.g. due to CLI tool's Erlang cookie not matching that of the server)\n * Target node is not running\n\nIn addition to the diagnostics info below:\n\n * See the CLI, clustering and networking guides on http://rabbitmq.com/documentation.html to learn more\n * Consult server logs on node test@c65c2bc59398\n\nDIAGNOSTICS\n===========\n\nattempted to contact:********@c65c2bc59398\n * effective user's home directory: /var/lib/rabbitmq\n * Erlang cookie hash: 3MxcYFrJzfhEL+FlUfLlQw==",
"stderr": [...],
"stderr_lines": [
"Error: unable to perform an operation on node 'test@c65c2bc59398'. Please see diagnostics information and suggestions below.", "",
"Most common reasons for this are:", "",
" * Target node is unreachable (e.g. due to hostname resolution, TCP connection or firewall issues)",
" * CLI tool fails to authenticate with the server (e.g. due to CLI tool's Erlang cookie not matching that of the server)",
" * Target node is not running", "",
"In addition to the diagnostics info below:", "",
" * See the CLI, clustering and networking guides on http://rabbitmq.com/documentation.html to learn more",
" * Consult server logs on node test@c65c2bc59398", "",
"DIAGNOSTICS", "===========", "",
"attempted to contact: [test@c65c2bc59398]", "",
"test@c65c2bc59398:",
" * connected to epmd (port 4369) on c65c2bc59398",
" * epmd reports: node 'test' not running at all",
" other nodes on c65c2bc59398: [rabbit]",
" * suggestion: start the node", "",
"Current node details:",
" * node name: rabbitmqcli2@c65c2bc59398",
" * effective user's home directory: /var/lib/rabbitmq",
" * Erlang cookie hash: 3MxcYFrJzfhEL+FlUfLlQw==", ""
],
"stdout": "",
"stdout_lines": []
}
* node parameter: fix default value
'rabbit' is the default value mentioned in the module documentation.
2018-10-08 22:34:08 +00:00
|
|
|
if self.node:
|
2016-05-25 20:15:00 +00:00
|
|
|
cmd.extend(['-n', self.node])
|
2014-09-26 01:01:01 +00:00
|
|
|
rc, out, err = self.module.run_command(cmd + args, check_rc=True)
|
|
|
|
return out.splitlines()
|
|
|
|
return list()
|
|
|
|
|
|
|
|
def get(self):
|
|
|
|
users = self._exec(['list_users'], True)
|
|
|
|
|
|
|
|
for user_tag in users:
|
2015-09-05 16:37:32 +00:00
|
|
|
if '\t' not in user_tag:
|
|
|
|
continue
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
user, tags = user_tag.split('\t')
|
|
|
|
|
|
|
|
if user == self.username:
|
2017-08-10 09:57:11 +00:00
|
|
|
for c in ['[', ']', ' ']:
|
2014-09-26 01:01:01 +00:00
|
|
|
tags = tags.replace(c, '')
|
|
|
|
|
|
|
|
if tags != '':
|
|
|
|
self._tags = tags.split(',')
|
|
|
|
else:
|
|
|
|
self._tags = list()
|
|
|
|
|
|
|
|
self._permissions = self._get_permissions()
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _get_permissions(self):
|
2018-12-07 13:19:08 +00:00
|
|
|
"""Get permissions of the user from RabbitMQ."""
|
2018-10-02 09:38:50 +00:00
|
|
|
perms_out = [perm for perm in self._exec(['list_user_permissions', self.username], True) if perm.strip()]
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2015-08-13 17:48:19 +00:00
|
|
|
perms_list = list()
|
2014-09-26 01:01:01 +00:00
|
|
|
for perm in perms_out:
|
|
|
|
vhost, configure_priv, write_priv, read_priv = perm.split('\t')
|
2015-08-13 17:48:19 +00: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
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-05-23 19:34:46 +00:00
|
|
|
def check_password(self):
|
|
|
|
return self._exec(['authenticate_user', self.username, self.password], True)
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def add(self):
|
2014-10-24 11:41:29 +00:00
|
|
|
if self.password is not None:
|
|
|
|
self._exec(['add_user', self.username, self.password])
|
2015-01-19 17:40:04 +00:00
|
|
|
else:
|
2014-10-24 11:41:29 +00:00
|
|
|
self._exec(['add_user', self.username, ''])
|
|
|
|
self._exec(['clear_password', self.username])
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
self._exec(['delete_user', self.username])
|
|
|
|
|
2018-05-23 19:34:46 +00:00
|
|
|
def change_password(self):
|
|
|
|
if self.password is not None:
|
|
|
|
self._exec(['change_password', self.username, self.password])
|
|
|
|
else:
|
|
|
|
self._exec(['clear_password', self.username])
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def set_tags(self):
|
|
|
|
self._exec(['set_user_tags', self.username] + self.tags)
|
|
|
|
|
|
|
|
def set_permissions(self):
|
2018-12-07 13:19:08 +00:00
|
|
|
permissions_to_clear = [permission for permission in self._permissions if permission not in self.permissions]
|
|
|
|
permissions_to_add = [permission for permission in self.permissions if permission not in self._permissions]
|
|
|
|
for permission in permissions_to_clear:
|
|
|
|
cmd = 'clear_permissions -p {vhost} {username}'.format(username=self.username,
|
|
|
|
vhost=permission['vhost'])
|
|
|
|
self._exec(cmd.split(' '))
|
|
|
|
for permission in permissions_to_add:
|
|
|
|
cmd = ('set_permissions -p {vhost} {username} {configure_priv} {write_priv} {read_priv}'
|
|
|
|
.format(username=self.username, **permission))
|
|
|
|
self._exec(cmd.split(' '))
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
def has_tags_modifications(self):
|
|
|
|
return set(self.tags) != set(self._tags)
|
|
|
|
|
|
|
|
def has_permissions_modifications(self):
|
2018-12-07 13:19:08 +00:00
|
|
|
def to_permission_tuple(vhost_permission_dict):
|
|
|
|
return vhost_permission_dict['vhost'], vhost_permission_dict
|
|
|
|
|
|
|
|
def permission_dict(vhost_permission_list):
|
|
|
|
return dict(map(to_permission_tuple, vhost_permission_list))
|
|
|
|
|
|
|
|
return permission_dict(self._permissions) != permission_dict(self.permissions)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-08-10 09:57:11 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def main():
|
|
|
|
arg_spec = dict(
|
|
|
|
user=dict(required=True, aliases=['username', 'name']),
|
2017-02-10 20:13:59 +00:00
|
|
|
password=dict(default=None, no_log=True),
|
2014-09-26 01:01:01 +00:00
|
|
|
tags=dict(default=None),
|
2016-02-15 21:11:02 +00:00
|
|
|
permissions=dict(default=list(), type='list'),
|
2014-09-26 01:01:01 +00:00
|
|
|
vhost=dict(default='/'),
|
|
|
|
configure_priv=dict(default='^$'),
|
|
|
|
write_priv=dict(default='^$'),
|
|
|
|
read_priv=dict(default='^$'),
|
|
|
|
force=dict(default='no', type='bool'),
|
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
rabbitmq_user: 'node' parameter: add default value (#38156)
* Remove unnecessary workaround
* add test: set RABBITMQ_NODENAME environment variable
The following error occurs:
TASK [rabbitmq_user : Add user] ***
fatal: [testhost]: FAILED! => {
"changed": false,
"cmd": "/usr/sbin/rabbitmqctl -q list_users",
"rc": 69,
"msg": "Error:********@c65c2bc59398'. Please see diagnostics information and suggestions below.\n\nMost common reasons for this are:\n\n * Target node is unreachable (e.g. due to hostname resolution, TCP connection or firewall issues)\n * CLI tool fails to authenticate with the server (e.g. due to CLI tool's Erlang cookie not matching that of the server)\n * Target node is not running\n\nIn addition to the diagnostics info below:\n\n * See the CLI, clustering and networking guides on http://rabbitmq.com/documentation.html to learn more\n * Consult server logs on node test@c65c2bc59398\n\nDIAGNOSTICS\n===========\n\nattempted to contact:********@c65c2bc59398\n * effective user's home directory: /var/lib/rabbitmq\n * Erlang cookie hash: 3MxcYFrJzfhEL+FlUfLlQw==",
"stderr": [...],
"stderr_lines": [
"Error: unable to perform an operation on node 'test@c65c2bc59398'. Please see diagnostics information and suggestions below.", "",
"Most common reasons for this are:", "",
" * Target node is unreachable (e.g. due to hostname resolution, TCP connection or firewall issues)",
" * CLI tool fails to authenticate with the server (e.g. due to CLI tool's Erlang cookie not matching that of the server)",
" * Target node is not running", "",
"In addition to the diagnostics info below:", "",
" * See the CLI, clustering and networking guides on http://rabbitmq.com/documentation.html to learn more",
" * Consult server logs on node test@c65c2bc59398", "",
"DIAGNOSTICS", "===========", "",
"attempted to contact: [test@c65c2bc59398]", "",
"test@c65c2bc59398:",
" * connected to epmd (port 4369) on c65c2bc59398",
" * epmd reports: node 'test' not running at all",
" other nodes on c65c2bc59398: [rabbit]",
" * suggestion: start the node", "",
"Current node details:",
" * node name: rabbitmqcli2@c65c2bc59398",
" * effective user's home directory: /var/lib/rabbitmq",
" * Erlang cookie hash: 3MxcYFrJzfhEL+FlUfLlQw==", ""
],
"stdout": "",
"stdout_lines": []
}
* node parameter: fix default value
'rabbit' is the default value mentioned in the module documentation.
2018-10-08 22:34:08 +00:00
|
|
|
node=dict(default='rabbit'),
|
2018-05-23 19:34:46 +00:00
|
|
|
update_password=dict(default='on_create', choices=['on_create', 'always'])
|
2014-09-26 01:01:01 +00: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 17:48:19 +00:00
|
|
|
permissions = module.params['permissions']
|
2014-09-26 01:01:01 +00:00
|
|
|
vhost = module.params['vhost']
|
|
|
|
configure_priv = module.params['configure_priv']
|
|
|
|
write_priv = module.params['write_priv']
|
|
|
|
read_priv = module.params['read_priv']
|
|
|
|
force = module.params['force']
|
|
|
|
state = module.params['state']
|
|
|
|
node = module.params['node']
|
2018-05-23 19:34:46 +00:00
|
|
|
update_password = module.params['update_password']
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-12-07 13:19:08 +00:00
|
|
|
if permissions:
|
|
|
|
vhosts = map(lambda permission: permission.get('vhost', '/'), permissions)
|
|
|
|
if any(map(lambda count: count > 1, count(vhosts).values())):
|
|
|
|
module.fail_json(msg="Error parsing permissions: You can't have two permission dicts for the same vhost")
|
|
|
|
bulk_permissions = True
|
|
|
|
else:
|
2015-08-13 17:48:19 +00:00
|
|
|
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)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-08-10 09:57:11 +00:00
|
|
|
result = dict(changed=False, user=username, state=state)
|
2014-09-26 01:01:01 +00:00
|
|
|
if rabbitmq_user.get():
|
|
|
|
if state == 'absent':
|
|
|
|
rabbitmq_user.delete()
|
2017-08-10 09:57:11 +00:00
|
|
|
result['changed'] = True
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
|
|
|
if force:
|
|
|
|
rabbitmq_user.delete()
|
|
|
|
rabbitmq_user.add()
|
|
|
|
rabbitmq_user.get()
|
2017-08-10 09:57:11 +00:00
|
|
|
result['changed'] = True
|
2018-05-23 19:34:46 +00:00
|
|
|
elif update_password == 'always':
|
|
|
|
if not rabbitmq_user.check_password():
|
|
|
|
rabbitmq_user.change_password()
|
|
|
|
result['changed'] = True
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
if rabbitmq_user.has_tags_modifications():
|
|
|
|
rabbitmq_user.set_tags()
|
2017-08-10 09:57:11 +00:00
|
|
|
result['changed'] = True
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
if rabbitmq_user.has_permissions_modifications():
|
|
|
|
rabbitmq_user.set_permissions()
|
2017-08-10 09:57:11 +00:00
|
|
|
result['changed'] = True
|
2014-09-26 01:01:01 +00:00
|
|
|
elif state == 'present':
|
|
|
|
rabbitmq_user.add()
|
|
|
|
rabbitmq_user.set_tags()
|
|
|
|
rabbitmq_user.set_permissions()
|
2017-08-10 09:57:11 +00:00
|
|
|
result['changed'] = True
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-08-10 09:57:11 +00:00
|
|
|
module.exit_json(**result)
|
2016-12-05 16:24:50 +00:00
|
|
|
|
2018-07-29 11:46:06 +00:00
|
|
|
|
2016-12-05 16:24:50 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|