ansible-collections / community.rabbitmq

Manage RabbitMQ with Ansible
http://galaxy.ansible.com/community/rabbitmq
Other
30 stars 46 forks source link

Please provide docs example of how to create shovel using rabbitmq_parameter #153

Closed mitchellroe closed 5 months ago

mitchellroe commented 1 year ago
SUMMARY

Is it possible to create a shovel configuration using the rabbitmq_parameter module? If so, I cannot find a way to do it. If this is supported, could the docs please be updated to include an example?

ISSUE TYPE
COMPONENT NAME

rabbitmq_parameter

ANSIBLE VERSION
ansible [core 2.14.3]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/mitchell.roe/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.10/site-packages/ansible
  ansible collection location = /home/mitchell.roe/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.10.10 (main, Mar  5 2023, 22:26:53) [GCC 12.2.1 20230201] (/usr/bin/python)
  jinja version = 3.1.2
  libyaml = True
mitchellroe commented 1 year ago

Related: https://github.com/ansible-collections/community.rabbitmq/issues/111

Im0 commented 1 year ago

G'day @mitchellroe , I've never used shovel before but reading https://www.rabbitmq.com/shovel-dynamic.html and the example it gave:

# my-shovel here is the name of the shovel
rabbitmqctl set_parameter shovel my-shovel \
  '{"src-protocol": "amqp091", "src-uri": "amqp://", "src-queue": "source-queue", "dest-protocol": "amqp091", "dest-uri": "amqp://remote-server", "dest-queue": "target-queue"}'

The code (https://github.com/ansible-collections/community.rabbitmq/blob/main/plugins/modules/rabbitmq_parameter.py#L94):

    def set(self):
        self._exec(['set_parameter',
                    '-p',
                    self.vhost,
                    self.component,
                    self.name,
                    json.dumps(self.value)])

I suppose the playbook task would look something like:

- name: Create shovel
    community.rabbitmq.rabbitmq_parameter:
    component: shovel
    name: my-shovel
    value: '{"src-protocol": "amqp091", "src-uri": "amqp://", "src-queue": "source-queue", "dest-protocol": "amqp091", "dest-uri": "amqp://remote-server", "dest-queue": "target-queue"}'
    state: present

I've not tested this in any way. Is there any chance you could try it to see if it caters to your needs and let us know?

mitchellroe commented 1 year ago

Hmm... Interesting.

When I was trying to use it for a production setup, it was failing with a JSON parsing error.

When I tried your example, it worked.

I will try tweaking your example, parameter by parameter, to find out where the issue lies.

Im0 commented 1 year ago

Hmm... Interesting.

When I was trying to use it for a production setup, it was failing with a JSON parsing error.

When I tried your example, it worked.

I will try tweaking your example, parameter by parameter, to find out where the issue lies.

Hi @mitchellroe let us know how you get on :) Just want some guidance on what the example could look like.

mitchellroe commented 1 year ago

@Im0 It turns out the issue was not with setting up a shovel using the rabbitmq_parameter module per-se, but with embedding variables into the value block.

For example, this works:

- name: create a test shovel using rabbitmq_parameter
  community.rabbitmq.rabbitmq_parameter:
    component: shovel
    name: mitchells_shovel
    value: '{
        "src-protocol": "amqp091",
        "src-uri": "amqp:///notifications",
        "src-queue": "mitchells_queue",
        "dest-protocol": "amqp091",
        "dest-uri": "amqp://mitchell:sup3rSecret@rabbitmq-amqp/notifications",
        "dest-exchange": "mitchells_exchange",
        "dest-add-forward-headers": false,
        "ack-mode": "on-confirm",
        "src-delete-after": "never"
      }'
    state: present

But this fails:

- name: create a test shovel using rabbitmq_parameter
  vars:
    shovel_user: mitchell
    shovel_pass: sup3rSecret
  community.rabbitmq.rabbitmq_parameter:
    component: shovel
    name: mitchells_shovel
    value: '{
        "src-protocol": "amqp091",
        "src-uri": "amqp:///notifications",
        "src-queue": "mitchells_queue",
        "dest-protocol": "amqp091",
        "dest-uri": "amqp://{{ shovel_user }}:{{ shovel_pass }}@rabbitmq-amqp/notifications",
        "dest-exchange": "mitchells_exchange",
        "dest-add-forward-headers": false,
        "ack-mode": "on-confirm",
        "src-delete-after": "never"
      }'
    state: present

Here is the error message from the module:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
fatal: [rabbitmq-amqp]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"}, "changed": false, "module_stderr": "Shared connection to rabbitmq-amqp closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n  File \"/home/mitchell.roe/.ansible/tmp/ansible-tmp-1681317145.1200414-71986-228926022745682/AnsiballZ_rabbitmq_parameter.py\", line 107, in <module>\r\n    _ansiballz_main()\r\n  File \"/home/mitchell.roe/.ansible/tmp/ansible-tmp-1681317145.1200414-71986-228926022745682/AnsiballZ_rabbitmq_parameter.py\", line 99, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/home/mitchell.roe/.ansible/tmp/ansible-tmp-1681317145.1200414-71986-228926022745682/AnsiballZ_rabbitmq_parameter.py\", line 48, in invoke_module\r\n    run_name='__main__', alter_sys=True)\r\n  File \"/usr/lib/python3.6/runpy.py\", line 205, in run_module\r\n    return _run_module_code(code, init_globals, run_name, mod_spec)\r\n  File \"/usr/lib/python3.6/runpy.py\", line 96, in _run_module_code\r\n    mod_name, mod_spec, pkg_name, script_name)\r\n  File \"/usr/lib/python3.6/runpy.py\", line 85, in _run_code\r\n    exec(code, run_globals)\r\n  File \"/tmp/ansible_community.rabbitmq.rabbitmq_parameter_payload__h3svbw1/ansible_community.rabbitmq.rabbitmq_parameter_payload.zip/ansible_collections/community/rabbitmq/plugins/modules/rabbitmq_parameter.py\", line 155, in <module>\r\n  File \"/tmp/ansible_community.rabbitmq.rabbitmq_parameter_payload__h3svbw1/ansible_community.rabbitmq.rabbitmq_parameter_payload.zip/ansible_collections/community/rabbitmq/plugins/modules/rabbitmq_parameter.py\", line 127, in main\r\n  File \"/usr/lib/python3.6/json/__init__.py\", line 354, in loads\r\n    return _default_decoder.decode(s)\r\n  File \"/usr/lib/python3.6/json/decoder.py\", line 339, in decode\r\n    obj, end = self.raw_decode(s, idx=_w(s, 0).end())\r\n  File \"/usr/lib/python3.6/json/decoder.py\", line 355, in raw_decode\r\n    obj, end = self.scan_once(s, idx)\r\njson.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
mitchellroe commented 1 year ago

I'm considering, at this point, just doing the following (untested):

- name: create a test shovel using rabbitmq_parameter
  vars:
    shovel_user: mitchell
    shovel_pass: sup3rSecret
    shovel_value:
      src-protocol: "amqp091"
      src-uri: "amqp:///notifications"
      src-queue: "mitchells_queue"
      dest-protocol: "amqp091"
      dest-uri: "amqp://{{ shovel_user }}:{{ shovel_pass }}@rabbitmq-amqp/notifications"
      dest-exchange: "mitchells_exchange"
      dest-add-forward-headers: false
      ack-mode: "on-confirm"
      src-delete-after: "never"
  community.rabbitmq.rabbitmq_parameter:
    component: shovel
    name: mitchells_shovel
    value: "{{ shovel_value | to_json }}"
    state: present

Edit: I have tested this method, and it seems to work.

mitchellroe commented 1 year ago

The reason these need to be variables, by the way, is that I'm actually going to be looping over a list of shovels that need to be created.

ilbarone87 commented 1 year ago

@mitchellroe i'm trying to do the same but with federation. Need to create federation link for several vhosts. Issue is that I cannot use {{ item }} in the value parameter since it's erroring out as you. Have you found a solution by any chance?

mitchellroe commented 1 year ago

@ilbarone87 I have tested the method outlined here (https://github.com/ansible-collections/community.rabbitmq/issues/153#issuecomment-1505598886), and it does seem to work. If it works for shovels, then it should ostensibly work for federations as well, @ilbarone87.

csmart commented 5 months ago

Has this been resolved by commit dd954c60ec9753664735bdaeee81e5e9e2a1ce64?

mitchellroe commented 5 months ago

Has this been resolved by commit dd954c6?

Yes, I believe so. Thanks!