openssh-formula/openssh/gather_host_keys.sls
alxwr aa3da8f2c2 Pillar openssh.known_hosts_salt_ssh (#128)
* Pillar openssh.known_hosts_salt_ssh

* Dropped ill-named file

* Fixed aliasing of host names

* Improved pillar.example

* Opt-in to include localhost

* pillar/known_hosts_salt_ssh: clear cache in run()

* Dropped forgotten debugging output
2018-06-01 14:11:52 +02:00

37 lines
1.1 KiB
Plaintext

{%- set minions = salt.slsutil.renderer(opts['config_dir'] + '/roster') %}
{%- set cache_dir = opts['cachedir'] + '/../master/known_hosts_salt_ssh' %}
{%- set cmd = "cat /etc/ssh/ssh_host_*_key.pub 2>/dev/null" %}
{{ cache_dir }}:
file.directory:
- makedirs: True
{%- for minion_id in minions %}
{%- set salt_ssh_cmd = "salt-ssh --out=json --static '{}' cmd.run_all '{}'".format(minion_id, cmd) %}
{%- set result = salt['cmd.run_all'](salt_ssh_cmd,
python_shell=True,
runas=salt['pillar.get']('openssh:known_hosts:salt_ssh:user', 'salt-master')
)
%}
{%- set pubkeys = False %}
{%- if result['retcode'] == 0 %}
{%- load_json as inner_result %}
{{ result['stdout'] }}
{%- endload %}
{%- set pubkeys = inner_result[minion_id]['stdout'].splitlines() | sort | join("\n") %}
{%- else %}
{%- do salt.log.error("{} failed: {}".format(salt_ssh_cmd, result)) %}
{%- endif %}
{%- if pubkeys %}
{{ cache_dir }}/{{ minion_id }}.pub:
file.managed:
- contents: |
{{ pubkeys | indent(8) }}
- require:
- file: {{ cache_dir }}
{%- endif %}
{%- endfor %}