---

# Use this playbook to copy all slurm users's ssh keys from an old
# master to new master(s)

- hosts: sync_ssh_keys_from_host
  tasks:
    - name: fetch current ssh keys
      fetch:
        src: "/home/{{ item }}/.ssh/authorized_keys"
        dest: "/tmp/keys/{{ item }}.keys"
        flat: yes
      become: yes
      with_items: "{{ slurm_users }}"
      tags: fetch

- hosts: sync_ssh_keys_to_hosts
  tasks:
    - name: mkdir .ssh
      file:
        dest: "/home/{{ item }}/.ssh"
        state: directory
        owner: "{{ item }}"
        group: "{{ item }}"
        mode: "0700"
      become: yes
      tags: mkdir
      with_items: "{{ slurm_users }}"
    - name: install ssh keys
      copy:
        src: "/tmp/keys/{{ item }}.keys"
        dest: "/home/{{ item }}/.ssh/authorized_keys"
        owner: "{{ item }}"
        group: "{{ item }}"
        mode: "0600"
      failed_when: no # only fails if there were no keys fetched from to_host
      become: yes
      tags: copy
      with_items: "{{ slurm_users }}"
