---
- name: set use_systemd
  set_fact:
    use_systemd: True
  when: (ansible_distribution == "CentOS" or ansible_distribution == "RedHat") and
          ( ansible_distribution_major_version == "7")

- name: set slurmd_enabled (default enabled)
  set_fact:
    slurmd_enabled: True
  when: slurmd_enabled is not defined and start_slurmd

- name: install slurmdbd initt
  template: src=slurmdbd.initd.j2 dest=/etc/init.d/slurmdbd mode=755
  become: true
  when: use_systemd is not defined and start_slurmdbd is defined

- name:  copy slurmdbd init script if OS contains systemd
  template: dest=/etc/systemd/system/slurmdbd.service src=slurmdbd.service.j2 mode=644
  become: true
  when: use_systemd is defined and start_slurmdbd is defined
  register: slurmdbd_service_installed

- name: copy slurm init script
  template: dest=/etc/init.d/slurm src=slurm.initd.j2  mode=755
  become: true
  when: use_systemd is not defined

- name:  copy slurmd.service
  template: dest=/etc/systemd/system/slurmd.service src=slurmd.service.j2 mode=644
  become: true
  when: use_systemd is defined and start_slurmd is defined
  register: slurmd_service_installed

- name: slurmctld.service
  template: dest=/etc/systemd/system/slurmctld.service src=slurmctld.service.j2 mode=644
  become: true
  when: use_systemd is defined and start_slurmctld is defined
  register: slurmctld_service_installed

- name: reload systemd after slurmd install
  systemd:
    daemon_reload: yes
  become: true
  when: use_systemd is defined and start_slurmd is defined and slurmd_service_installed.changed

- name: reload systemd after slurmctld _service _installed
  systemd:
    daemon_reload: yes
  become: true
  when: use_systemd is defined and start_slurmctld is defined and slurmctld_service_installed.changed

- name: reload systemd slurmdbd_ service _installed
  systemd:
    daemon_reload: yes
  become: true
  when: use_systemd is defined and start_slurmdbd is defined and slurmdbd_service_installed.changed

- name: make sure munge is started
  service: name=munge state=started enabled=yes
  become: true
  when: use_systemd is defined and start_slurmdbd is defined

- name: start munge
  service: name=munge state=restarted enabled=yes
  become: true
  when: use_systemd is defined and ( slurmdbd_service_installed.changed or slurmctld_service_installed.changed or slurmd_service_installed.changed)

- name: start slurmdbd
  service: name=slurmdbd state=started enabled={{ start_slurmdbd }}
  become: true
  when: start_slurmdbd is defined

- name: restart slurmdbd
  service: name=slurmdbd state=restarted enabled={{ start_slurmdbd }}
  become: true
  when: start_slurmdbd is defined and slurmdbd_service_installed.changed

- name: start slurmctl
  service: name=slurmctld state=started enabled={{ start_slurmctld }}
  become: true
  when: use_systemd is defined and start_slurmctld is defined

- name: restart slurmctl
  service: name=slurmctld state=restarted enabled={{ start_slurmctld }}
  become: true
  when: use_systemd is defined and start_slurmctld is defined and slurmctld_service_installed.changed

- name: "count clusters in slurm db"
  shell:  "{{ slurm_dir }}/bin/sacctmgr show cluster -p | wc -l"
  register: slurm_cluster_count
  check_mode: no
  changed_when: false

- debug:
    var: slurm_cluster_count

- name: "create cluster in slurm db"  #needs munge to run
  shell:  "{{ slurm_dir }}/bin/sacctmgr -i create cluster {{ clustername }}"
  become: true
  when: slurm_cluster_count.stdout == '1' and slurmctrl == inventory_hostname

- name: start slurmd
  service: name=slurmd state=started enabled={{ slurmd_enabled }}
  become: true
  when: use_systemd is defined and start_slurmd is defined

- name: restart slurmd
  service: name=slurmd state=restarted enabled={{ slurmd_enabled }}
  become: true
  when: use_systemd is defined and start_slurmd is defined and slurmd_service_installed.changed

- name: start slurm
  service: name=slurm state=restarted enabled={{ slurmd_enabled }}
  become: true
  when: use_systemd is not defined and ( start_slurmd is defined or start_slurmctld is defined )
