---

- name: compile per-host user list for host
  set_fact:
    relevant_users: "{{ users | users_having_access(hostvars[inventory_hostname]) }}"
  changed_when: False

#- debug:
#    msg:
#      matching_keys: "{{ relevant_users.values() | map(attribute='ssh_keys') | map(attribute='pffile') | list }}"

- assert:
    that:
    - relevant_users | length > 0

- name: compile per-host ssh key list
  set_fact:
    relevant_ssh_keys: "{{
			relevant_users.values() | map(attribute='ssh_keys') | map(attribute='pffile') | list
			+
			hostvars[inventory_hostname]['extra_ssh_keys'] | default([])
		}}"

- name: Deploy SSH-Keys to remote host
  authorized_key:
    user: "{{ ansible_ssh_user | default('perfact') }}"
    key: "{{ relevant_ssh_keys | join('\n') }}"
    exclusive: true
  when: (not no_sshd | default(false))
