summaryrefslogtreecommitdiffstats
path: root/ssh.yml
blob: 43d8d2d8f17a713d50e1e72624b3849365d853b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- name: Store known hosts of 'all' the hosts in the inventory file
  hosts: all:localhost
  connection: local
  tasks:
  - delegate_to: "localhost"
    set_fact: target_hosts="{{ play_hosts | difference(['localhost']) }}"
    
 
- 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: "{{ target_hosts }}"

  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 of {{ item.item }} 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 }}"