---
- name: Install dependencies
  become: yes
  ansible.builtin.apt:
    name: "{{ rabbitmq_dependencies }}"
    state: present

- name: Download and install RabbitMQ keys
  become: yes
  ansible.builtin.shell: |
    curl -1sLf "{{ item.url }}" | gpg --dearmor | tee {{ item.dest }} > /dev/null
  args:
    creates: "{{ item.dest }}"
  loop: "{{ rabbitmq_keys }}"

- name: Add RabbitMQ APT repositories
  become: yes
  ansible.builtin.apt_repository:
    repo: "{{ item }}"
    state: present
    update_cache: no
  loop: "{{ rabbitmq_repositories }}"

- name: Update package cache after adding repositories
  become: yes
  ansible.builtin.apt:
    update_cache: yes

- name: Install Erlang packages
  become: yes
  ansible.builtin.apt:
    name: "{{ rabbitmq_erlang_dependencies }}"
    state: present

- name: Install RabbitMQ server
  become: yes
  ansible.builtin.apt:
    name: rabbitmq-server
    state: present
