---

- name: Deploy rabbitmq config file
  ansible.builtin.template:
    src: rabbitmq.conf.j2
    dest: /etc/rabbitmq/rabbitmq.conf
    mode: "0644"
    owner: rabbitmq
    group: rabbitmq
  notify: Restart Rabbitmq
  tags: config

- name: Deploy rabbitmq env file
  ansible.builtin.template:
    src: rabbitmq-env.conf.j2
    dest: /etc/rabbitmq/rabbitmq-env.conf
    mode: "0644"
    owner: rabbitmq
    group: rabbitmq
  notify: Restart Rabbitmq
  tags: config

- name: Enabling Rabbitmq plugins
  community.rabbitmq.rabbitmq_plugin:
    name: "{{ rabbitmq_plugins }}"
    state: enabled
  tags: config

- name: Delete guest user
  community.rabbitmq.rabbitmq_user:
    user: guest
    state: absent
  tags: config

- name: Create vhosts
  community.rabbitmq.rabbitmq_vhost:
    name: "{{ item }}"
    state: present
  with_items: "{{ rabbitmq_vhosts }}"
  tags: config,users,vhosts

- name: Create admin user
  community.rabbitmq.rabbitmq_user:
    user: "{{ rabbitmq_admin_username }}"
    password: "{{ rabbitmq_admin_password }}"
    vhost: "{{ item }}"
    configure_priv: .*
    read_priv: .*
    write_priv: .*
    state: present
    tags: administrator
  tags: config
  with_items: "{{ rabbitmq_vhosts }}"

- name: Create checkmk user
  community.rabbitmq.rabbitmq_user:
    user: "{{ rabbitmq_checkmk_username }}"
    password: "{{ rabbitmq_checkmk_password }}"
    vhost: /
    configure_priv: ""
    read_priv: .*
    write_priv: ""
    state: present
    tags: monitoring
  tags: config

- name: Create exchanges
  community.rabbitmq.rabbitmq_exchange:
    name: "{{ item.name }}"
    type: "{{ item.type }}"
    vhost: "{{ item.vhost }}"
    state: present
    login_user: "{{ rabbitmq_admin_username }}"
    login_password: "{{ rabbitmq_admin_password }}"
  with_items: "{{ rabbitmq_exchanges }}"
  tags: config,exchanges

- name: Create queues
  community.rabbitmq.rabbitmq_queue:
    login_user: "{{ rabbitmq_admin_username }}"
    login_password: "{{ rabbitmq_admin_password }}"
    state: "{{ item.state | default('present') }}"
    vhost: "{{ item.vhost }}"
    name: "{{ item.name }}"
    durable: "{{ item.durable | default(true) }}"
    dead_letter_exchange: "{{ item.dead_letter_exchange | default() }}"
    dead_letter_routing_key: "{{ item.dead_letter_routing_key | default() }}"
    arguments: "{{ item.arguments | default({}) }}"
  with_items: "{{ rabbitmq_queues }}"
  tags: config,queues

- name: Create bindings
  community.rabbitmq.rabbitmq_binding:
    login_user: "{{ rabbitmq_admin_username }}"
    login_password: "{{ rabbitmq_admin_password }}"
    state: "{{ item.state | default('present') }}"
    vhost: "{{ item.vhost }}"
    name: "{{ item.name }}"
    destination: "{{ item.destination }}"
    destination_type: "{{ item.destination_type }}"
    routing_key: "{{ item.routing_key }}"
    arguments: "{{ item.arguments | default({}) }}"
  with_items: "{{ rabbitmq_bindings }}"
  tags: config,bindings

- name: Create app users
  community.rabbitmq.rabbitmq_user:
    user: "{{ item.username }}"
    password: "{{ item.password }}"
    vhost: "{{ item.vhost }}"
    read_priv: "{{ item.read_priv | default('.*') }}"
    write_priv: "{{ item.write_priv | default('.*') }}"
    configure_priv: "{{ item.configure_priv | default('.*') }}"
    state: present
    tags: monitoring,management
  with_items: "{{ rabbitmq_app_users }}"
  tags: config,users
