Created
April 22, 2021 12:47
-
-
Save fessmage/e09354ec4df9cec012dd2246005c0d27 to your computer and use it in GitHub Desktop.
Add all hosts from ansible inventory to ssh known_hosts file of current user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: Gather facts from 'all' hosts in inventory | |
hosts: all | |
vars: | |
ansible_host_key_checking: false | |
ansible_ssh_extra_args: '-o UserKnownHostsFile=/dev/null' | |
tasks: | |
- setup: | |
gather_subset: network | |
- name: Add public keys to known_hosts file | |
hosts: localhost | |
connection: local | |
vars: | |
ssh_known_hosts_file: "{{ lookup('env','HOME') + '/.ssh/known_hosts' }}" | |
ssh_known_hosts: "{{ groups['all'] }}" | |
tasks: | |
- known_hosts: | |
path: "{{ ssh_known_hosts_file }}" | |
name: "{{ item }}" | |
key: "{{ lookup('pipe','ssh-keyscan -T 10 ' + item + ',' + lookup('dig',item)) }}" | |
state: present | |
loop: "{{ ssh_known_hosts | map('extract', hostvars, ['ansible_fqdn']) | list }}" | |
become: no |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what I needed to get it to work