From e799b6ece925de7bb39ed32d4f5e2a478a76e364 Mon Sep 17 00:00:00 2001
From: Chris Hines <chris.hines@monash.edu>
Date: Sun, 19 Jun 2016 23:06:01 +0000
Subject: [PATCH] rework of the known_hosts template for simplicity and
 robustness

---
 .../templates/known_hosts.j2                  | 24 ++++++-------------
 1 file changed, 7 insertions(+), 17 deletions(-)

diff --git a/roles/calculateKnownHosts/templates/known_hosts.j2 b/roles/calculateKnownHosts/templates/known_hosts.j2
index 09eff179..38400fff 100644
--- a/roles/calculateKnownHosts/templates/known_hosts.j2
+++ b/roles/calculateKnownHosts/templates/known_hosts.j2
@@ -1,22 +1,12 @@
-{% set nodelist = [] %}
+{% set keytypes = [ { 'type': 'ssh-rsa', 'fact': 'ansible_ssh_host_key_rsa_public' }, { 'type': 'ecdsa-sha2-nistp256', 'fact': 'ansible_ssh_host_key_ecdsa_public'} ] %}
 {% for node in groups['all'] %}
-{% for interface in hostvars[node]['ansible_interfaces'] %}
-{% if interface != "lo" %}
-{% if 'ansible_ssh_host_key_rsa_public' in hostvars[node]  %}
-{% set host = {'name': node, 'ip': hostvars[node]['ansible_'+interface]['ipv4']['address'], 'keytype':'ssh-rsa', 'key': hostvars[node]['ansible_ssh_host_key_rsa_public']} %}
-{% if nodelist.append(host) %}
-{% endif %}
-{% endif %}
-{% if 'ansible_ssh_host_key_ecdsa_public' inĂ‚ hostvars[node] %}
-{% set host = {'name': node, 'ip': hostvars[node]['ansible_'+interface]['ipv4']['address'], 'keytype':'ecdsa-sha2-nistp256', 'key': hostvars[node]['ansible_ssh_host_key_ecdsa_public']} %}
-{% if nodelist.append(host) %}
-{% endif %}
-{% endif %}
+{% for hostkey in keytypes %}
+{% if hostkey['fact'] in hostvars[node] %}
+{{ node }} {{ hostkey['type'] }} {{ hostvars[node][hostkey['fact']] }}
+{% for ip in hostvars[node]['ansible_all_ipv4_addresses'] %}
+{{ ip }} {{ hostkey['type'] }} {{ hostvars[node][hostkey['fact']] }}
+{% endfor %}
 {% endif %}
 {% endfor %}
 {% endfor %}
 
-{% for host in nodelist %}
-{{ host.ip }} {{ host.keytype }} {{ host.key }}
-{{ host.name }} {{ host.keytype }} {{ host.key }}
-{% endfor %}
-- 
GitLab