| 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 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
--- |
| 13 |
module: rabbitmq_vhost |
| 14 |
short_description: Manage the state of a virtual host in RabbitMQ |
| 15 |
description: |
| 16 |
- Manage the state of a virtual host in RabbitMQ using rabbitmqctl or REST APIs. |
| 17 |
author: Chris Hoffman (@chrishoffman) |
| 18 |
options: |
| 19 |
name: |
| 20 |
description: |
| 21 |
- The name of the vhost to manage |
| 22 |
type: str |
| 23 |
required: true |
| 24 |
aliases: [vhost] |
| 25 |
node: |
| 26 |
description: |
| 27 |
- erlang node name of the rabbit we wish to configure |
| 28 |
type: str |
| 29 |
default: rabbit |
| 30 |
tracing: |
| 31 |
description: |
| 32 |
- Enable/disable tracing for a vhost |
| 33 |
type: bool |
| 34 |
default: false |
| 35 |
aliases: [trace] |
| 36 |
state: |
| 37 |
description: |
| 38 |
- The state of vhost |
| 39 |
type: str |
| 40 |
default: present |
| 41 |
choices: [present, absent] |
| 42 |
login_user: |
| 43 |
description: |
| 44 |
- RabbitMQ user for connection. |
| 45 |
type: str |
| 46 |
version_added: '1.5.0' |
| 47 |
login_password: |
| 48 |
description: |
| 49 |
- RabbitMQ password for connection. |
| 50 |
type: str |
| 51 |
version_added: '1.5.0' |
| 52 |
login_host: |
| 53 |
description: |
| 54 |
- RabbitMQ host for connection. |
| 55 |
type: str |
| 56 |
version_added: '1.5.0' |
| 57 |
login_port: |
| 58 |
description: |
| 59 |
- RabbitMQ management API port. |
| 60 |
type: str |
| 61 |
default: '15672' |
| 62 |
version_added: '1.5.0' |
| 63 |
login_protocol: |
| 64 |
description: |
| 65 |
- RabbitMQ management API protocol. |
| 66 |
type: str |
| 67 |
choices: [ http , https ] |
| 68 |
default: http |
| 69 |
version_added: '1.5.0' |
| 70 |
ca_cert: |
| 71 |
description: |
| 72 |
- CA certificate to verify SSL connection to management API. |
| 73 |
type: path |
| 74 |
version_added: '1.5.0' |
| 75 |
client_cert: |
| 76 |
description: |
| 77 |
- Client certificate to send on SSL connections to management API. |
| 78 |
type: path |
| 79 |
version_added: '1.5.0' |
| 80 |
client_key: |
| 81 |
description: |
| 82 |
- Private key matching the client certificate. |
| 83 |
type: path |
| 84 |
version_added: '1.5.0' |
| 85 |
""" |
| 86 |
|
| 87 |
EXAMPLES = r""" |
| 88 |
- name: Ensure that the vhost /test exists. |
| 89 |
community.rabbitmq.rabbitmq_vhost: |
| 90 |
name: /test |
| 91 |
state: present |
| 92 |
|
| 93 |
- name: Ensure that the vhost /test exists using REST APIs. |
| 94 |
community.rabbitmq.rabbitmq_vhost: |
| 95 |
name: /test |
| 96 |
state: present |
| 97 |
login_host: localhost |
| 98 |
login_user: admin |
| 99 |
login_password: changeadmin |
| 100 |
""" |
| 101 |
|
| 102 |
import traceback |
| 103 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 104 |
from ansible.module_utils.six.moves.urllib import parse as urllib_parse |
| 105 |
|
| 106 |
REQUESTS_IMP_ERR = None |
| 107 |
try: |
| 108 |
import requests |
| 109 |
HAS_REQUESTS = True |
| 110 |
except ImportError: |
| 111 |
REQUESTS_IMP_ERR = traceback.format_exc() |
| 112 |
HAS_REQUESTS = False |
| 113 |
|
| 114 |
|
| 115 |
class RabbitMqVhost(object): |
| 116 |
def __init__( |
| 117 |
self, |
| 118 |
module, |
| 119 |
name, |
| 120 |
tracing, |
| 121 |
node, |
| 122 |
login_user, |
| 123 |
login_password, |
| 124 |
login_host, |
| 125 |
login_port, |
| 126 |
login_protocol, |
| 127 |
ca_cert, |
| 128 |
client_cert, |
| 129 |
client_key, |
| 130 |
): |
| 131 |
self.module = module |
| 132 |
self.name = name |
| 133 |
self.tracing = tracing |
| 134 |
self.node = node |
| 135 |
self.login_user = login_user |
| 136 |
self.login_password = login_password |
| 137 |
self.login_host = login_host |
| 138 |
self.login_port = login_port |
| 139 |
self.login_protocol = login_protocol |
| 140 |
self.verify = ca_cert |
| 141 |
self.cert = client_cert |
| 142 |
self.key = client_key |
| 143 |
|
| 144 |
self._tracing = False |
| 145 |
require_rabbitmqctl = self.login_host is None |
| 146 |
self._rabbitmqctl = module.get_bin_path("rabbitmqctl", require_rabbitmqctl) |
| 147 |
|
| 148 |
def _exec(self, args, force_exec_in_check_mode=False): |
| 149 |
if not self.module.check_mode or ( |
| 150 |
self.module.check_mode and force_exec_in_check_mode |
| 151 |
): |
| 152 |
cmd = [self._rabbitmqctl, "-q", "-n", self.node] |
| 153 |
rc, out, err = self.module.run_command(cmd + args, check_rc=True) |
| 154 |
return out.splitlines() |
| 155 |
return list() |
| 156 |
|
| 157 |
def get(self): |
| 158 |
if self.login_host is not None: |
| 159 |
response = self._request_vhost_api("get") |
| 160 |
|
| 161 |
if response.ok: |
| 162 |
self._tracing = response.json()["tracing"] |
| 163 |
return True |
| 164 |
elif response.status_code == 404: |
| 165 |
return False |
| 166 |
else: |
| 167 |
self.module.fail_json( |
| 168 |
msg="Error getting the vhost", |
| 169 |
status=response.status_code, |
| 170 |
details=response.text, |
| 171 |
) |
| 172 |
else: |
| 173 |
vhosts = self._exec(["list_vhosts", "name", "tracing"], True) |
| 174 |
|
| 175 |
for vhost in vhosts: |
| 176 |
if "\t" not in vhost: |
| 177 |
continue |
| 178 |
|
| 179 |
name, tracing = vhost.split("\t") |
| 180 |
if name == self.name: |
| 181 |
self._tracing = self.module.boolean(tracing) |
| 182 |
return True |
| 183 |
return False |
| 184 |
|
| 185 |
def add(self): |
| 186 |
if self.login_host is not None: |
| 187 |
response = self._request_vhost_api("put") |
| 188 |
|
| 189 |
if response is not None and not response.ok: |
| 190 |
msg = ( |
| 191 |
"Error trying to create vhost %s in rabbitmq. " "Status code '%s'." |
| 192 |
) % (self.name, response.status_code) |
| 193 |
self.module.fail_json(msg=msg) |
| 194 |
else: |
| 195 |
return self._exec(["add_vhost", self.name]) |
| 196 |
|
| 197 |
def delete(self): |
| 198 |
if self.login_host is not None: |
| 199 |
response = self._request_vhost_api("delete") |
| 200 |
|
| 201 |
if response is not None and not response.ok: |
| 202 |
msg = ( |
| 203 |
"Error trying to remove vhost %s in rabbitmq. " "Status code '%s'." |
| 204 |
) % (self.name, response.status_code) |
| 205 |
self.module.fail_json(msg=msg) |
| 206 |
else: |
| 207 |
return self._exec(["delete_vhost", self.name]) |
| 208 |
|
| 209 |
def set_tracing(self): |
| 210 |
if self.tracing != self._tracing: |
| 211 |
if self.tracing: |
| 212 |
self._enable_tracing() |
| 213 |
else: |
| 214 |
self._disable_tracing() |
| 215 |
return True |
| 216 |
return False |
| 217 |
|
| 218 |
def _enable_tracing(self): |
| 219 |
if self.login_host is not None: |
| 220 |
response = self._request_vhost_api("put", data={"tracing": True}) |
| 221 |
|
| 222 |
if response is not None and not response.ok: |
| 223 |
msg = ( |
| 224 |
"Error trying to enable tracing on vhost %s in rabbitmq. " |
| 225 |
"Status code '%s'." |
| 226 |
) % (self.name, response.status_code) |
| 227 |
self.module.fail_json(msg=msg) |
| 228 |
else: |
| 229 |
return self._exec(["trace_on", "-p", self.name]) |
| 230 |
|
| 231 |
def _disable_tracing(self): |
| 232 |
if self.login_host is not None: |
| 233 |
response = self._request_vhost_api("put", data={"tracing": False}) |
| 234 |
|
| 235 |
if response is not None and not response.ok: |
| 236 |
msg = ( |
| 237 |
"Error trying to disable tracing on vhost %s in rabbitmq. " |
| 238 |
"Status code '%s'." |
| 239 |
) % (self.name, response.status_code) |
| 240 |
self.module.fail_json(msg=msg) |
| 241 |
else: |
| 242 |
return self._exec(["trace_off", "-p", self.name]) |
| 243 |
|
| 244 |
def _request_vhost_api(self, method, data=None): |
| 245 |
if self.module.check_mode and method != "get": |
| 246 |
return None |
| 247 |
try: |
| 248 |
url = "%s://%s:%s/api/vhosts/%s" % ( |
| 249 |
self.login_protocol, |
| 250 |
self.login_host, |
| 251 |
self.login_port, |
| 252 |
# Ensure provided data is safe to use in a URL. |
| 253 |
# https://docs.python.org/3/library/urllib.parse.html#url-quoting |
| 254 |
# NOTE: This will also encode '/' characters, as they are required |
| 255 |
# to be percent encoded in the RabbitMQ management API. |
| 256 |
urllib_parse.quote(self.name, safe=''), |
| 257 |
) |
| 258 |
response = requests.request( |
| 259 |
method=method, |
| 260 |
url=url, |
| 261 |
auth=(self.login_user, self.login_password), |
| 262 |
verify=self.verify, |
| 263 |
cert=(self.cert, self.key), |
| 264 |
json=data, |
| 265 |
) |
| 266 |
|
| 267 |
except requests.exceptions.RequestException as exception: |
| 268 |
msg = "Error in HTTP request (method %s) for user %s in rabbitmq." % ( |
| 269 |
method, |
| 270 |
self.login_user, |
| 271 |
) |
| 272 |
self.module.fail_json(msg=msg, exception=exception) |
| 273 |
|
| 274 |
return response |
| 275 |
|
| 276 |
|
| 277 |
def main(): |
| 278 |
arg_spec = dict( |
| 279 |
name=dict(required=True, aliases=["vhost"]), |
| 280 |
tracing=dict(default="off", aliases=["trace"], type="bool"), |
| 281 |
state=dict(default="present", choices=["present", "absent"]), |
| 282 |
node=dict(default="rabbit"), |
| 283 |
login_user=dict(type="str", no_log=True), |
| 284 |
login_password=dict(type="str", no_log=True), |
| 285 |
login_host=dict(type="str"), |
| 286 |
login_port=dict(type="str", default="15672"), |
| 287 |
login_protocol=dict(type="str", default="http", choices=["http", "https"]), |
| 288 |
ca_cert=dict(type="path"), |
| 289 |
client_cert=dict(type="path"), |
| 290 |
client_key=dict(type="path"), |
| 291 |
) |
| 292 |
|
| 293 |
module = AnsibleModule(argument_spec=arg_spec, supports_check_mode=True) |
| 294 |
|
| 295 |
name = module.params["name"] |
| 296 |
tracing = module.params["tracing"] |
| 297 |
state = module.params["state"] |
| 298 |
node = module.params["node"] |
| 299 |
login_user = module.params["login_user"] |
| 300 |
login_password = module.params["login_password"] |
| 301 |
login_host = module.params["login_host"] |
| 302 |
login_port = module.params["login_port"] |
| 303 |
login_protocol = module.params["login_protocol"] |
| 304 |
ca_cert = module.params["ca_cert"] |
| 305 |
client_cert = module.params["client_cert"] |
| 306 |
client_key = module.params["client_key"] |
| 307 |
|
| 308 |
if not HAS_REQUESTS: |
| 309 |
module.fail_json(msg=missing_required_lib("requests"), exception=REQUESTS_IMP_ERR) |
| 310 |
|
| 311 |
result = dict(changed=False, name=name, state=state) |
| 312 |
rabbitmq_vhost = RabbitMqVhost( |
| 313 |
module, |
| 314 |
name, |
| 315 |
tracing, |
| 316 |
node, |
| 317 |
login_user, |
| 318 |
login_password, |
| 319 |
login_host, |
| 320 |
login_port, |
| 321 |
login_protocol, |
| 322 |
ca_cert, |
| 323 |
client_cert, |
| 324 |
client_key, |
| 325 |
) |
| 326 |
|
| 327 |
if rabbitmq_vhost.get(): |
| 328 |
if state == "absent": |
| 329 |
rabbitmq_vhost.delete() |
| 330 |
result["changed"] = True |
| 331 |
else: |
| 332 |
if rabbitmq_vhost.set_tracing(): |
| 333 |
result["changed"] = True |
| 334 |
elif state == "present": |
| 335 |
rabbitmq_vhost.add() |
| 336 |
rabbitmq_vhost.set_tracing() |
| 337 |
result["changed"] = True |
| 338 |
|
| 339 |
module.exit_json(**result) |
| 340 |
|
| 341 |
|
| 342 |
if __name__ == "__main__": |
| 343 |
main() |