---

- name: Download Slurm source
  get_url:
    url: "{{ slurm_url }}"
    dest: /root/Downloads/
    checksum: "{{ slurm_md5 }}" 
  tags: install

- name: Build SLURM RPMs
  command: rpmbuild -ta /root/Downloads/slurm-20.02.0.tar.bz2 
  tags: install

- name: Copy RPMs to NFS share
  copy:
    src: "{{ item }}" 
    dest: /home/rpms/
  with_fileglob:
    - /root/rpmbuild/RPMS/x86_64/slurm*20*.rpm
  tags: install

- name: Install SLURM RPMs on Master
  yum: 
    name: "{{ item }}"
    #name: "{{ query('fileglob', ['/home/rpms/slurm*20*.rpm']) }}" <-- how it should work to avoid loop
  with_fileglob:
    - /home/rpms/slurm*20*.rpm
  tags: install

- name: Firewall Rule slurm allow 6817/tcp
  command: firewall-cmd  --zone=internal --add-port=6817/tcp --permanent
  tags: install

- name: Firewall Rule slurm allow 6818/tcp
  command: firewall-cmd  --zone=internal --add-port=6818/tcp --permanent
  tags: install

- name: Firewall Rule slurm allow 6819/tcp
  command: firewall-cmd  --zone=internal --add-port=6819/tcp --permanent
  tags: install

- name: Firewall Rule slurm allow all incoming traffic on internal network
  command: firewall-cmd --permanent --zone=internal --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" accept'
  tags: install

- name: Firewall Reload
  command: firewall-cmd  --reload
  tags: install


- name: Start MariaDB 
  service:
    name: mariadb
    state: restarted
    enabled: yes
  tags: install

- name: Grant Permissions for SLURM DB
  command: mysql -u root -e "GRANT ALL ON slurm_acct_db.* TO 'slurm'@'localhost' identified by 'password' with grant option;"
  tags: install

- name: Create slurmdbd.conf file
  copy:
    src: /etc/slurm/slurmdbd.conf.example
    dest: /etc/slurm/slurmdbd.conf
    mode: 0600
  tags: install

- name: Populate Accounting Database
  command: slurmdbd
  tags: install

- name: Create Slurm Cluster
  command: sacctmgr -i add cluster {{inventory_hostname}}
  tags: install

- name: Create Default Slurm Group
  command: sacctmgr -i add account defaultgroup Cluster={{inventory_hostname}} Description="Default Account" Organization="Default Org"
  tags: install

- name: Add root to the Default Account 
  command: sacctmgr -i add user root DefaultAccount=defaultgroup
  tags: install

- name: Start slurmctld on Master
  service: 
    name: slurmctld
    state: restarted
    enabled: yes
  tags: install

- name: Enable Slurmdbd on Master
  service: 
    name: slurmdbd
    state: restarted
    enabled: yes
  tags: install

