---
- name: Ensuring config directories exist
  file:
    path: "{{ node_config_directory }}/{{ project_name }}"
    state: "directory"
    owner: "{{ config_owner_user }}"
    group: "{{ config_owner_group }}"
    mode: "0770"
  become: true
  when:
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool
  with_dict: "{{ rabbitmq_services }}"

- name: Copying over config.json files for services
  template:
    src: "{{ item.key }}.json.j2"
    dest: "{{ node_config_directory }}/{{ project_name }}/config.json"
    mode: "0770"
  become: true
  register: rabbitmq_config_jsons
  when:
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool
  with_dict: "{{ rabbitmq_services }}"
  notify:
    - Restart rabbitmq container

- name: Copying over rabbitmq configs
  vars:
    service: "{{ rabbitmq_services['rabbitmq'] }}"
  template:
    src: "{{ item }}.j2"
    dest: "{{ node_config_directory }}/{{ project_name }}/{{ item }}"
    mode: "0770"
  become: true
  register: rabbitmq_confs
  when:
    - inventory_hostname in groups[service.group]
    - service.enabled | bool
  with_items:
    - "rabbitmq-env.conf"
    - "rabbitmq.config"
    - "rabbitmq-clusterer.config"
    - "definitions.json"
  notify:
    - Restart rabbitmq container

- name: Check rabbitmq containers
  become: true
  kolla_docker:
    action: "compare_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ item.value.container_name }}"
    image: "{{ item.value.image }}"
    volumes: "{{ item.value.volumes }}"
    environment: "{{ item.value.environment }}"
    dimensions: "{{ item.value.dimensions }}"
  register: check_rabbitmq_containers
  when:
    - kolla_action != "config"
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool
  with_dict: "{{ rabbitmq_services }}"
  notify:
    - Restart rabbitmq container
