ansible-collections / community.rabbitmq

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

Fix rabbitmq_policy tags json.loads - Issue #28 #86

Closed nazunalika closed 3 years ago

nazunalika commented 3 years ago

Redoing PR based on feedback in #58

SUMMARY

Fixes #28 by trying to load with json.loads and if unable, to assign tags without loading as json.

ISSUE TYPE
COMPONENT NAME

rabbitmq_policy

ADDITIONAL INFORMATION

During the _policy_check piece of this module, policy_data is basically a list based on a split of the variable policy. However, policy does not contain data. As a result of the split, it becomes:

['vhost', 'name', 'pattern', 'apply-to', 'definition', 'priority']

As there is no JSON data, there is no reason to use json.loads on a string.

To reproduce this issue:

  1. Install the ansible collection using ansible-galaxy
  2. Create a playbook with a set of tasks that deal with rabbitmq policies on a vhost. This example is on a Red Hat system. Example tasks below.
- name: Configure pubsub virtual host
  community.rabbitmq.rabbitmq_vhost:
    name: /pubsub
    state: present

- name: Configure an HA policy for pubsub
  community.rabbitmq.rabbitmq_policy:
    name: HA
    apply_to: queues
    pattern: ".*"
    tags:
      ha-mode: all
      ha-sync-mode: automatic
      ha-sync-batch-size: 10000
    vhost: /pubsub
  1. Run the playbook
# BEFORE
The full traceback is:
Traceback (most recent call last):
  File "/home/ansible/.ansible/tmp/ansible-tmp-1611654783.6917102-146428-116716834350778/AnsiballZ_rabbitmq_policy.py", line 102, in <module>
    _ansiballz_main()
  File "/home/ansible/.ansible/tmp/ansible-tmp-1611654783.6917102-146428-116716834350778/AnsiballZ_rabbitmq_policy.py", line 94, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/home/ansible/.ansible/tmp/ansible-tmp-1611654783.6917102-146428-116716834350778/AnsiballZ_rabbitmq_policy.py", line 40, in invoke_module
    runpy.run_module(mod_name='ansible_collections.community.rabbitmq.plugins.modules.rabbitmq_policy', init_globals=None, run_name='__main__', alter_sys=True)
  File "/usr/lib64/python3.6/runpy.py", line 205, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib64/python3.6/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/tmp/ansible_community.rabbitmq.rabbitmq_policy_payload__4toqeqo/ansible_community.rabbitmq.rabbitmq_policy_payload.zip/ansible_collections/community/rabbitmq/plugins/modules/rabbitmq_policy.py", line 248, in <module>
  File "/tmp/ansible_community.rabbitmq.rabbitmq_policy_payload__4toqeqo/ansible_community.rabbitmq.rabbitmq_policy_payload.zip/ansible_collections/community/rabbitmq/plugins/modules/rabbitmq_policy.py", line 237, in main
  File "/tmp/ansible_community.rabbitmq.rabbitmq_policy_payload__4toqeqo/ansible_community.rabbitmq.rabbitmq_policy_payload.zip/ansible_collections/community/rabbitmq/plugins/modules/rabbitmq_policy.py", line 161, in has_modifications
  File "/tmp/ansible_community.rabbitmq.rabbitmq_policy_payload__4toqeqo/ansible_community.rabbitmq.rabbitmq_policy_payload.zip/ansible_collections/community/rabbitmq/plugins/modules/rabbitmq_policy.py", line 161, in <genexpr>
  File "/tmp/ansible_community.rabbitmq.rabbitmq_policy_payload__4toqeqo/ansible_community.rabbitmq.rabbitmq_policy_payload.zip/ansible_collections/community/rabbitmq/plugins/modules/rabbitmq_policy.py", line 198, in _policy_check
  File "/usr/lib64/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

#AFTER
TASK [rockylinux.rabbitmq : Configure the HA policy for pubsub] *********************************************
changed: [rabbitmq001.example.com]
Andersson007 commented 3 years ago

@nazunalika hi, thanks for the new PR! Strange errors.. As i can see there can be some issues with this https://stackoverflow.com/questions/15170676/cannot-import-jsondecodeerror. Let's try to change JSONDecodeError to ValueError. If it doesn't help, to just except Exception? I think it should be also fine.

Andersson007 commented 3 years ago

Ah, i see now.. First please try to put json.decoder.JSONDecodeError instead JSONDecodeError, then, if doesn't help, what i suggested above. Thanks!

Andersson007 commented 3 years ago

@nazunalika thanks! Green now! Does this solve your problem locally? If yes, can we merge the PR?

Andersson007 commented 3 years ago

cc @odyssey4me

Andersson007 commented 3 years ago

@nazunalika thanks for your contribution (bugfix + tests)! I think now we can release the collection even tomorrow, not on Friday as was planned. Please take a look at the open issues and pull requests. Any help will be much appreciated!

@odyssey4me thanks for reviewing and approving!