--- - name: get ssh keys for all users ansible.builtin.find: paths: "{{ sshd_keys_paths }}" file_type: link recurse: true delegate_to: localhost run_once: true register: sshd_keys_result_find - name: set sshd_keys_found_users variable ansible.builtin.set_fact: sshd_keys_found_users: "{{ sshd_keys_result_find.files | map(attribute='path') | map('dirname') | map('basename') | unique }}" - name: create groups for users with ssh keys ansible.builtin.group: name: "{{ item }}" state: present loop: "{{ sshd_keys_found_users }}" - name: create users with ssh keys ansible.builtin.user: name: "{{ item }}" group: "{{ item }}" state: present loop: "{{ sshd_keys_found_users }}" - name: synchronize ssh keys ansible.posix.authorized_key: user: "{{ item.path | dirname | basename }}" state: present key: "{{ lookup('file', item.path) }}" loop_control: label: "{{ item.path }}: {{ item.path | dirname | basename }}" loop: "{{ sshd_keys_result_find.files }}" failed_when: false