---

- name: Remove unused config
  file: path=/etc/rabbitmq/rabbitmq.config state=absent
  when: rabbitmq_conf == 'rabbit.conf'
  notify: restart rabbitmq-server
  tags: rabbit

# Create a self signed cert. This is needed even if a cert is later obtained with dehydrated as
# rabbitmq must be started before that
- import_tasks: ../includes/create_selfsigned_cert.yml
  vars:
    - cert_path: /etc/rabbitmq/ssl/cert.pem
    - cert_key_path: /etc/rabbitmq/ssl/key.pem
    - cert_key_user: rabbitmq
  tags: rabbitmq

- name: Check if the cert chain exists
  stat: path=/etc/rabbitmq/ssl/chain.pem
  register: rabbitmq_ssl_chain
  tags: rabbitmq

- name: Copy the cert on the chain file
  copy: src=/etc/rabbitmq/ssl/cert.pem dest=/etc/rabbitmq/ssl/chain.pem remote_src=True
  when: not rabbitmq_ssl_chain.stat.exists
  tags: rabbitmq

- name: Deploy configuration
  template: src={{ rabbitmq_conf }}.j2 dest=/etc/rabbitmq/{{ rabbitmq_conf }}
  notify: restart rabbitmq-server
  tags: rabbitmq

- name: Deploy plugins to enable
  template: src=enabled_plugins.j2 dest=/etc/rabbitmq/enabled_plugins
  notify: restart rabbitmq-server
  tags: rabbitmq
