From 32c7fa7f7084ea1f766328b22c01974818136975 Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Sun, 10 Feb 2013 14:32:43 -0500 Subject: [PATCH 1/9] Remove unneccesary quotes from user task --- examples/playbooks/rabbitmq.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/playbooks/rabbitmq.yml b/examples/playbooks/rabbitmq.yml index 0c0656199a..c205c6bd78 100644 --- a/examples/playbooks/rabbitmq.yml +++ b/examples/playbooks/rabbitmq.yml @@ -20,7 +20,7 @@ - restart rabbitmq - name: add users - rabbitmq_user: user=$item password=changeme tags=monitoring,$item vhost="/" configure_priv=".*" write_priv=".*" read_priv=".*" state=present + rabbitmq_user: user=$item password=changeme tags=administrator,$item vhost=/ configure_priv=.* write_priv=.* read_priv=.* state=present with_items: - user1 - user2 @@ -34,4 +34,3 @@ handlers: - name: restart rabbitmq service: name=rabbitmq-server state=restarted - From 5a4ff6ca601a087c433f77ea5b3bca598e86fc87 Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Sun, 10 Feb 2013 14:37:21 -0500 Subject: [PATCH 2/9] Cleaning up tag compare, getting fully qualified path for executable --- library/rabbitmq_user | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/library/rabbitmq_user b/library/rabbitmq_user index 12c9804867..d0291ce287 100644 --- a/library/rabbitmq_user +++ b/library/rabbitmq_user @@ -106,6 +106,7 @@ class RabbitMqUser(object): self._tags = None self._permissions = None + self._rabbitmqctl = module.get_bin_path("rabbitmqctl", True) def _exec(self, args): cmd = ["rabbitmqctl", "-q"] @@ -147,7 +148,6 @@ class RabbitMqUser(object): if not self.module.check_mode: self._exec(["add_user", self.username, self.password]) - def delete(self): if not self.module.check_mode: self._exec(["delete_user", self.username]) @@ -168,18 +168,7 @@ class RabbitMqUser(object): self._exec(cmd) def has_tags_modifications(self): - if (not self._tags and len(self.tags) > 0) or (not self.tags and len(self._tags) > 0): - return True - else: - for tag in self._tags: - if tag not in self.tags: - return True - - for tag in self.tags: - if tag not in self._tags: - return True - - return False + return set(self.tags) != set(self._tags) def has_permissions_modifications(self): return self._permissions != self.permissions @@ -238,7 +227,7 @@ def main(): rabbitmq_user.set_permissions() changed = True - module.exit_json(changed=changed) + module.exit_json(changed=changed, user=username, state=state) # this is magic, see lib/ansible/module_common.py #<> From c94ec20a1694359941e2bb778a2c23670fce08b5 Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Sun, 10 Feb 2013 14:38:00 -0500 Subject: [PATCH 3/9] Getting fully qualified path for executable --- library/rabbitmq_plugin | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/rabbitmq_plugin b/library/rabbitmq_plugin index 0fe0d91a5b..6e724c743f 100644 --- a/library/rabbitmq_plugin +++ b/library/rabbitmq_plugin @@ -54,9 +54,10 @@ examples: class RabbitMqPlugins(object): def __init__(self, module): self.module = module + self._rabbitmq_plugins = module.get_bin_path("rabbitmq-plugins", True) def _exec(self, args): - cmd = ["rabbitmq-plugins"] + cmd = [self._rabbitmq_plugins] rc, out, err = self.module.run_command(cmd + args, check_rc=True) return out.splitlines() From b79d7a606ceaeee9a4a227445611acc90f96752c Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Sun, 10 Feb 2013 17:00:09 -0500 Subject: [PATCH 4/9] Using new qualified executable --- library/rabbitmq_user | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/rabbitmq_user b/library/rabbitmq_user index d0291ce287..88c8385344 100644 --- a/library/rabbitmq_user +++ b/library/rabbitmq_user @@ -92,7 +92,7 @@ class RabbitMqUser(object): self.username = username self.password = password if tags is None: - self.tags = [] + self.tags = list() else: self.tags = tags.split(',') @@ -109,7 +109,7 @@ class RabbitMqUser(object): self._rabbitmqctl = module.get_bin_path("rabbitmqctl", True) def _exec(self, args): - cmd = ["rabbitmqctl", "-q"] + cmd = [self._rabbitmqctl, "-q"] rc, out, err = self.module.run_command(cmd + args, check_rc=True) return out.splitlines() @@ -126,7 +126,7 @@ class RabbitMqUser(object): if tags != '': self._tags = tags.split(',') else: - self._tags = [] + self._tags = list() self._permissions = self._get_permissions() From 31e5e81fcd63f8908983328e544c2273b35b7c5a Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Sun, 10 Feb 2013 17:01:20 -0500 Subject: [PATCH 5/9] Standarizing module in line with other rabbitmq modules, adding support for setting tracing --- library/rabbitmq_vhost | 109 ++++++++++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 34 deletions(-) diff --git a/library/rabbitmq_vhost b/library/rabbitmq_vhost index 39c8524af3..13b36b4cc5 100644 --- a/library/rabbitmq_vhost +++ b/library/rabbitmq_vhost @@ -31,62 +31,103 @@ options: - The name of the vhost to manage required: true default: null + tracing: + description: + Enable/disable tracing for a vhost + default: no + choices: [yes, no] state: description: - The state of vhost - required: true - default: null - choices: [ "present", "absent" ] + default: present + choices: [present, absent] examples: - - code: "rabbitmq_vhost: name=/test state=present" + - code: 'rabbitmq_vhost: name=/test state=present' description: Ensure that the vhost /test exists. author: Matt Cordial ''' +class RabbitMqVhost(object): + def __init__(self, module, name, tracing): + self.module = module + self.name = name + self.tracing = tracing + + 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): + cmd = [self._rabbitmqctl, '-q'] + 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]) + def main(): arg_spec = dict( name=dict(required=True), - state=dict(required=False, choices=['present', 'absent']) + tracing=dict(default='off', choices=BOOLEANS), + state=dict(default='present', choices=['present', 'absent']) ) - module = AnsibleModule(argument_spec=arg_spec) + module = AnsibleModule( + argument_spec=arg_spec, + supports_check_mode=True + ) name = module.params['name'] + tracing = module.boolean(module.params['tracing']) state = module.params['state'] - RABBITMQCTL = module.get_bin_path('rabbitmqctl', True) + rabbitmq_vhost = RabbitMqVhost(module, name, tracing) - present = False - rc, out, err = module.run_command('%s list_vhosts' % RABBITMQCTL) - for line in out.splitlines(): - if line.strip() == name: - present = True - break - - if state == 'present' and present: - module.exit_json(changed=False, name=name, state=state) - - if state == 'present' and not present: - rc, out, err = module.run_command('%s add_vhost %s' % (RABBITMQCTL, name)) - if '...done' in out: - module.exit_json(changed=True, name=name, state=state) + changed = False + if rabbitmq_vhost.get(): + if state == 'absent': + rabbitmq_vhost.delete() + changed = True else: - module.fail_json(msg=out, name=name, state=state) + if rabbitmq_vhost.set_tracing(): + changed = True + elif state == 'present': + rabbitmq_vhost.add() + rabbitmq_vhost.set_tracing() + changed = True - if state == 'absent' and not present: - module.exit_json(changed=False, name=name, state=state) - - if state == 'absent' and present: - rc, out, err = module.run_command('%s delete_vhost %s' % (RABBITMQCTL, name)) - if '...done' in out: - module.exit_json(changed=True, name=name, state=state) - else: - module.fail_json(msg=out, name=name, state=state) - - module.exit_json(changed=False, name=name, state=state) + module.exit_json(changed=changed, name=name, state=state) # this is magic, see lib/ansible/module_common.py #<> - main() From 114a4dc5978c74e6620c5deb29d1598b151b0176 Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Sun, 10 Feb 2013 17:02:50 -0500 Subject: [PATCH 6/9] Standarizing quotes --- library/rabbitmq_user | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/library/rabbitmq_user b/library/rabbitmq_user index 88c8385344..ab8e9b00f3 100644 --- a/library/rabbitmq_user +++ b/library/rabbitmq_user @@ -82,7 +82,7 @@ options: default: present choices: [present, absent] examples: - - code: rabbitmq_user user=joe password=changeme vhost="/" configure_priv=".*" read_priv=".*" write_priv=".*" state=present + - code: rabbitmq_user user=joe password=changeme vhost=/ configure_priv=.* read_priv=.* write_priv=.* state=present description: Add user to server and assign full access control ''' @@ -106,15 +106,15 @@ class RabbitMqUser(object): self._tags = None self._permissions = None - self._rabbitmqctl = module.get_bin_path("rabbitmqctl", True) + self._rabbitmqctl = module.get_bin_path('rabbitmqctl', True) def _exec(self, args): - cmd = [self._rabbitmqctl, "-q"] + cmd = [self._rabbitmqctl, '-q'] rc, out, err = self.module.run_command(cmd + args, check_rc=True) return out.splitlines() def get(self): - users = self._exec(["list_users"]) + users = self._exec(['list_users']) for user_tag in users: user, tags = user_tag.split('\t') @@ -135,7 +135,7 @@ class RabbitMqUser(object): return False def _get_permissions(self): - perms_out = self._exec(["list_user_permissions", self.username]) + perms_out = self._exec(['list_user_permissions', self.username]) for perm in perms_out: vhost, configure_priv, write_priv, read_priv = perm.split('\t') @@ -146,19 +146,19 @@ class RabbitMqUser(object): def add(self): if not self.module.check_mode: - self._exec(["add_user", self.username, self.password]) + self._exec(['add_user', self.username, self.password]) def delete(self): if not self.module.check_mode: - self._exec(["delete_user", self.username]) + self._exec(['delete_user', self.username]) def set_tags(self): if not self.module.check_mode: - self._exec(["set_user_tags", self.username] + self.tags) + self._exec(['set_user_tags', self.username] + self.tags) def set_permissions(self): if not self.module.check_mode: - cmd = ["set_permissions"] + cmd = ['set_permissions'] cmd.append('-p') cmd.append(self.permissions['vhost']) cmd.append(self.username) From 2c947c2ed66026da558d35444173902bb976bb2c Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Sun, 10 Feb 2013 17:08:33 -0500 Subject: [PATCH 7/9] Adding aliases --- library/rabbitmq_vhost | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/rabbitmq_vhost b/library/rabbitmq_vhost index 13b36b4cc5..31cf86de85 100644 --- a/library/rabbitmq_vhost +++ b/library/rabbitmq_vhost @@ -31,11 +31,13 @@ options: - The name of the vhost to manage required: true default: null + aliases: [vhost] tracing: description: Enable/disable tracing for a vhost default: no choices: [yes, no] + aliases: [trace] state: description: - The state of vhost @@ -97,8 +99,8 @@ class RabbitMqVhost(object): def main(): arg_spec = dict( - name=dict(required=True), - tracing=dict(default='off', choices=BOOLEANS), + name=dict(required=True, aliases=['vhost']), + tracing=dict(default='off', choices=BOOLEANS, aliases=['trace']), state=dict(default='present', choices=['present', 'absent']) ) From ca3b8228f76e730a14d067f09e93e86eaafca1eb Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Sun, 10 Feb 2013 17:14:24 -0500 Subject: [PATCH 8/9] Updating how to run commands in check mode, standarizing quotes --- library/rabbitmq_plugin | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/library/rabbitmq_plugin b/library/rabbitmq_plugin index 6e724c743f..73f3dc58ee 100644 --- a/library/rabbitmq_plugin +++ b/library/rabbitmq_plugin @@ -54,23 +54,21 @@ examples: class RabbitMqPlugins(object): def __init__(self, module): self.module = module - self._rabbitmq_plugins = module.get_bin_path("rabbitmq-plugins", True) + self._rabbitmq_plugins = module.get_bin_path('rabbitmq-plugins', True) - def _exec(self, args): + def _exec(self, args, run_in_check_mode=False): cmd = [self._rabbitmq_plugins] rc, out, err = self.module.run_command(cmd + args, check_rc=True) return out.splitlines() def get_all(self): - return self._exec(["list", "-E", "-m"]) + return self._exec(['list', '-E', '-m'], True) def enable(self, name): - if not self.module.check_mode: - self._exec(["enable", name]) + self._exec(['enable', name]) def disable(self, name): - if not self.module.check_mode: - self._exec(["disable", name]) + self._exec(['disable', name]) def main(): arg_spec = dict( From 507e49396b19c5467c6a3310c5b1e877f2272b99 Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Sun, 10 Feb 2013 17:20:44 -0500 Subject: [PATCH 9/9] Updating how to run commands in check mode --- library/rabbitmq_plugin | 8 +++++--- library/rabbitmq_user | 42 +++++++++++++++++++---------------------- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/library/rabbitmq_plugin b/library/rabbitmq_plugin index 73f3dc58ee..4bd549dc61 100644 --- a/library/rabbitmq_plugin +++ b/library/rabbitmq_plugin @@ -57,9 +57,11 @@ class RabbitMqPlugins(object): self._rabbitmq_plugins = module.get_bin_path('rabbitmq-plugins', True) def _exec(self, args, run_in_check_mode=False): - cmd = [self._rabbitmq_plugins] - rc, out, err = self.module.run_command(cmd + args, check_rc=True) - return out.splitlines() + if not self.module.check_mode or (self.module.check_mode and run_in_check_mode): + cmd = [self._rabbitmq_plugins] + rc, out, err = self.module.run_command(cmd + args, check_rc=True) + return out.splitlines() + return list() def get_all(self): return self._exec(['list', '-E', '-m'], True) diff --git a/library/rabbitmq_user b/library/rabbitmq_user index ab8e9b00f3..497ce29e5d 100644 --- a/library/rabbitmq_user +++ b/library/rabbitmq_user @@ -108,13 +108,15 @@ class RabbitMqUser(object): self._permissions = None self._rabbitmqctl = module.get_bin_path('rabbitmqctl', True) - def _exec(self, args): - cmd = [self._rabbitmqctl, '-q'] - rc, out, err = self.module.run_command(cmd + args, check_rc=True) - return out.splitlines() + 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): + cmd = [self._rabbitmqctl, '-q'] + 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']) + users = self._exec(['list_users'], True) for user_tag in users: user, tags = user_tag.split('\t') @@ -129,13 +131,11 @@ class RabbitMqUser(object): self._tags = list() self._permissions = self._get_permissions() - return True - return False def _get_permissions(self): - perms_out = self._exec(['list_user_permissions', self.username]) + perms_out = self._exec(['list_user_permissions', self.username], True) for perm in perms_out: vhost, configure_priv, write_priv, read_priv = perm.split('\t') @@ -145,27 +145,23 @@ class RabbitMqUser(object): return dict() def add(self): - if not self.module.check_mode: - self._exec(['add_user', self.username, self.password]) + self._exec(['add_user', self.username, self.password]) def delete(self): - if not self.module.check_mode: - self._exec(['delete_user', self.username]) + self._exec(['delete_user', self.username]) def set_tags(self): - if not self.module.check_mode: - self._exec(['set_user_tags', self.username] + self.tags) + self._exec(['set_user_tags', self.username] + self.tags) def set_permissions(self): - if not self.module.check_mode: - cmd = ['set_permissions'] - cmd.append('-p') - cmd.append(self.permissions['vhost']) - cmd.append(self.username) - cmd.append(self.permissions['configure_priv']) - cmd.append(self.permissions['write_priv']) - cmd.append(self.permissions['read_priv']) - self._exec(cmd) + cmd = ['set_permissions'] + cmd.append('-p') + cmd.append(self.permissions['vhost']) + cmd.append(self.username) + cmd.append(self.permissions['configure_priv']) + cmd.append(self.permissions['write_priv']) + cmd.append(self.permissions['read_priv']) + self._exec(cmd) def has_tags_modifications(self): return set(self.tags) != set(self._tags)