--- - name: gather public_keys hosts: frontal tasks: - block: - shell: "ssh-keyscan -t ecdsa -H {{ hostvars[item].ansible_hostname }} {{ hostvars[item].ansible_all_ipv4_addresses | join(',') }} {{ hostvars[item].inventory_hostname }},{{ hostvars[item].inventory_hostname_short }}" args: creates: 'host_vars/{{ hostvars[item].inventory_hostname_short }}.yml' register: ssh_keyscan with_items: "{{groups['all']}}" - copy: content: "---\nknown_hosts: \n{% for line in item.stdout_lines %} - '{{ line }}'\n{% endfor %}" dest: 'host_vars/{{ hostvars[item.item].ansible_hostname }}' force: no with_items: "{{ssh_keyscan.results}}" when: item.stdout_lines | length > 0