Saltstack Official OpenSSH Formula
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

72 lines
2.9KB

  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_expr_form)](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 expr_form = salt['pillar.get']('openssh:known_hosts:expr_form', '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_expr_form = salt['pillar.get']('openssh:known_hosts:hostnames:expr_form', '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, expr_form=expr_form) -%}
  59. {%- set host_names = salt['mine.get'](target, hostname_function, expr_form=expr_form) -%}
  60. {%- for host, keys in host_keys|dictsort -%}
  61. {{ known_host_entry(host, host_names, keys) }}
  62. {%- endfor -%}