Saltstack Official OpenSSH Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 line
3.0KB

  1. {#
  2. # vi:syntax=jinja
  3. #}
  4. {#- Generates one known_hosts entry per given key #}
  5. {%- macro known_host_entry(host, host_names, keys) %}
  6. {#- Get IPv4 and IPv6 addresses from the DNS #}
  7. {%- set ip4 = salt['dig.A'](host) -%}
  8. {%- set ip6 = salt['dig.AAAA'](host) -%}
  9. {#- The host names to use are to be found within the dict 'host_names'. #}
  10. {#- If there are none, the host is used directly. #}
  11. {%- set names = [host_names.get(host, host)] -%}
  12. {#- Extract the hostname from the FQDN and add it to the names. #}
  13. {%- if use_hostnames is iterable -%}
  14. {%- for name in names | sort -%}
  15. {%- if salt["match.{}".format(hostnames_tgt_type)](hostnames_target, minion_id=name) -%}
  16. {%- set hostname = name.split('.')|first -%}
  17. {%- if hostname not in names -%}
  18. {%- do names.append(hostname) -%}
  19. {%- endif -%}
  20. {%- endif -%}
  21. {%- endfor -%}
  22. {%- endif -%}
  23. {#- Append IP addresses and aliases (if they are not already present) #}
  24. {%- for ip in (ip4 + ip6)|sort -%}
  25. {%- do names.append(ip) -%}
  26. {%- for alias in aliases_ips.get(ip, []) -%}
  27. {%- if alias not in names -%}
  28. {%- do names.append(alias) -%}
  29. {%- endif -%}
  30. {%- endfor -%}
  31. {%- endfor -%}
  32. {#- Write one line per key; join the names together #}
  33. {%- for line in keys.split('\n') -%}
  34. {%- if line -%}
  35. {{ ','.join(names) }} {{ line }}
  36. {% endif -%}
  37. {%- endfor -%}
  38. {%- endmacro -%}
  39. {#- Pre-fetch pillar data #}
  40. {%- set target = salt['pillar.get']('openssh:known_hosts:target', '*') -%}
  41. {%- set tgt_type = salt['pillar.get']('openssh:known_hosts:tgt_type', 'glob') -%}
  42. {%- set keys_function = salt['pillar.get']('openssh:known_hosts:mine_keys_function', 'public_ssh_host_keys') -%}
  43. {%- set hostname_function = salt['pillar.get']('openssh:known_hosts:mine_hostname_function', 'public_ssh_hostname') -%}
  44. {%- set use_hostnames = salt['pillar.get']('openssh:known_hosts:hostnames', False) -%}
  45. {%- set hostnames_target_default = '*' if grains['domain'] == '' else "*.{}".format(grains['domain']) -%}
  46. {%- set hostnames_target = salt['pillar.get']('openssh:known_hosts:hostnames:target', hostnames_target_default) -%}
  47. {%- set hostnames_tgt_type = salt['pillar.get']('openssh:known_hosts:hostnames:tgt_type', 'glob') -%}
  48. {#- Lookup IP of all aliases so that when we have a matching IP, we inject the alias name
  49. in the SSH known_hosts entry -#}
  50. {%- set aliases = salt['pillar.get']('openssh:known_hosts:aliases', []) -%}
  51. {%- set aliases_ips = {} -%}
  52. {%- for alias in aliases -%}
  53. {%- for ip in salt['dig.A'](alias) + salt['dig.AAAA'](alias) -%}
  54. {%- do aliases_ips.setdefault(ip, []).append(alias) -%}
  55. {%- endfor -%}
  56. {%- endfor -%}
  57. {#- Loop over targetted minions -#}
  58. {%- set host_keys = salt['mine.get'](target, keys_function, tgt_type=tgt_type) -%}
  59. {%- set host_names = salt['mine.get'](target, hostname_function, tgt_type=tgt_type) -%}
  60. {%- do host_keys.update(salt['pillar.get']('openssh:known_hosts:static',
  61. {}).items()) -%}
  62. {%- for host, keys in host_keys| dictsort -%}
  63. {{ known_host_entry(host, host_names, keys) }}
  64. {%- endfor -%}