Saltstack Official OpenSSH Formula
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

104 行
4.3KB

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