---
- name: Ensure .ssh folder exists
  ansible.windows.win_file:
    path: "{{ ssh_folder }}"
    state: directory

- name: Copy SSH private keys
  ansible.windows.win_copy:
    src: "{{ item }}"
    dest: "{{ ssh_folder }}\\{{ item }}"
  loop: "{{ user.ssh_private_keys }}"
  when: (user.ssh_private_keys is defined) and (user.ssh_private_keys | bool)

- name: Copy SSH known hosts
  ansible.windows.win_copy:
    src: known_hosts
    dest: "{{ ssh_folder }}\\known_hosts"

- name: Copy SSH config
  ansible.windows.win_template:
    src: config.j2
    dest: "{{ ssh_folder }}\\config"

- name: Ensure proper permissions on the SSH keys
  ansible.windows.win_command: |
    Cmd /c Icacls {{ ssh_folder }}\\{{ item }} /c /t /Inheritance:d
    Cmd /c Icacls {{ ssh_folder }}\\{{ item }} /c /t /Grant %UserName%:F
    Cmd /c Icacls {{ ssh_folder }}\\{{ item }} /c /t /Remove Administrator BUILTIN\Administrators BUILTIN Everyone System Users "Authenticated Users"
  loop: "{{ user.ssh_private_keys }}"
  when: (user.ssh_private_keys is defined) and (user.ssh_private_keys | bool)
