Skip to content

Instantly share code, notes, and snippets.

@cbrunnkvist
Last active July 14, 2022 09:49
Show Gist options
  • Save cbrunnkvist/2a6090b35a9de7e8bac7c2f49ddb7409 to your computer and use it in GitHub Desktop.
Save cbrunnkvist/2a6090b35a9de7e8bac7c2f49ddb7409 to your computer and use it in GitHub Desktop.
SSH keyscan all Ansible inventory
---
# in retrospect, it's better to just run e.g.
# ansible all -a true --ssh-extra-args="-o UpdateHostKeys=yes -o StrictHostKeyChecking=accept-new"
- hosts: all
gather_facts: false
tasks:
- name: Set custom SSH port fact (or use default)
delegate_to: localhost
set_fact:
ansible_ssh_port: "{{ hostvars[inventory_hostname]['ansible_ssh_port'] | default('22') }}"
- name: Scan and save all SSH host keys
delegate_to: localhost
lineinfile:
dest: ~/.ssh/known_hosts
create: yes
state: present
line: "{{ lookup('pipe', 'ssh-keyscan -trsa -p' + ansible_ssh_port + ' ' + inventory_hostname) }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment