Skip to content

Instantly share code, notes, and snippets.

@FelikZ
Created March 13, 2018 14:00
Show Gist options
  • Save FelikZ/888f7f881b7b5ee853d00f88992b4c99 to your computer and use it in GitHub Desktop.
Save FelikZ/888f7f881b7b5ee853d00f88992b4c99 to your computer and use it in GitHub Desktop.
Ansible known_hosts fix. SSH, git clone, etc
# https://stackoverflow.com/a/39083724/734493
# original version
- name: Store known hosts of 'all' the hosts in the inventory file
hosts: localhost
connection: local
vars:
ssh_known_hosts_command: "ssh-keyscan -T 10"
ssh_known_hosts_file: "{{ lookup('env','HOME') + '/.ssh/known_hosts' }}"
ssh_known_hosts: "{{ groups['all'] }}"
tasks:
- name: For each host, scan for its ssh public key
shell: "ssh-keyscan {{ item }},`dig +short {{ item }}`"
with_items: "{{ ssh_known_hosts }}"
register: ssh_known_host_results
ignore_errors: yes
- name: Add/update the public key in the '{{ ssh_known_hosts_file }}'
known_hosts:
name: "{{ item.item }}"
key: "{{ item.stdout }}"
path: "{{ ssh_known_hosts_file }}"
with_items: "{{ ssh_known_host_results.results }}"
- name: For each host, scan for its ssh public key
shell: "ssh-keyscan {{ item }},`dig +short {{ item }}`"
with_items: "{{ known_hosts }}"
register: ssh_known_host_results
become_user: '{{ user }}'
ignore_errors: yes
- name: Add/update the public key in the known_hosts
known_hosts:
name: "{{ item.item }}"
key: "{{ item.stdout }}"
become_user: '{{ user }}'
with_items: "{{ ssh_known_host_results.results }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment