# This task generates a 4096-bit RSA OpenSSH keypair on svc-teststation. The public key is exported and saved on tester-control.

---
- name: Create SSH directory
  file:
    path: /home/{{ item }}/.ssh
    state: directory
    owner: "{{ item }}"
    mode: u+rw
  loop: '{{ users_list }}'

- name: Generating SSH keypair
  openssh_keypair:
    path: /home/{{ item }}/.ssh/id_rsa
    owner: "{{ item }}"
    size: 4096
    type: rsa
  loop: '{{ users_list }}'

- name: Change public key ownership
  file:
    path: /home/{{ item }}/.ssh/id_rsa.pub
    owner: "{{ item }}"
  loop: '{{ users_list }}'

- name: Register hostname for public key file name
  command: hostname
  register: hostname

- name: Fetch public key to tester-control
  fetch:
    src: /home/{{ item }}/.ssh/id_rsa.pub
    dest: /home/robot/public-keys/{{ item }}/{{ hostname.stdout }}/
    flat: yes
  loop: '{{ users_list }}'
