---
- block:
    - name: "{{ project_name }} | Ensure RabbitMQ vhosts exist"
      kolla_toolbox:
        container_engine: "{{ kolla_container_engine }}"
        module_name: rabbitmq_vhost
        module_args:
          name: "{{ item }}"
        user: rabbitmq
      loop: "{{ service_rabbitmq_users | map(attribute='vhost') | unique | reject('equalto', '/') | list }}"
      register: service_rabbitmq_result
      until: service_rabbitmq_result is success
      retries: "{{ service_rabbitmq_retries }}"
      delay: "{{ service_rabbitmq_delay }}"

    - name: "{{ project_name }} | Ensure RabbitMQ users exist"
      kolla_toolbox:
        container_engine: "{{ kolla_container_engine }}"
        module_name: rabbitmq_user
        module_args:
          user: "{{ item.user }}"
          password: "{{ item.password }}"
          node: "rabbit@{{ hostvars[service_rabbitmq_delegate_host]['ansible_facts']['hostname'] }}"
          update_password: always
          vhost: "{{ item.vhost }}"
          configure_priv: ".*"
          read_priv: ".*"
          tags: "{{ item.tags | default([]) | join(',') }}"
          write_priv: ".*"
        user: rabbitmq
      loop: "{{ service_rabbitmq_users }}"
      loop_control:
        label: "{{ item.user }} -> {{ item.vhost }}"
      register: service_rabbitmq_result
      until: service_rabbitmq_result is success
      retries: "{{ service_rabbitmq_retries }}"
      delay: "{{ service_rabbitmq_delay }}"
      no_log: True

  become: true
  when: service_rabbitmq_when | bool
  delegate_to: "{{ service_rabbitmq_delegate_host }}"
  run_once: "{{ service_rabbitmq_run_once }}"
  tags:
    - service-rabbitmq
