Skip to content

Commit ea221ab

Browse files
alxwrmyii
authored andcommitted
feat(ssh_known_hosts): allow to omit IP addresses
1 parent dfaeb8f commit ea221ab

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

docs/README.rst

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ setup those functions through pillar::
117117
public_ssh_host_keys:
118118
mine_function: cmd.run
119119
cmd: cat /etc/ssh/ssh_host_*_key.pub
120-
python_shell: True
120+
python_shell: true
121121
public_ssh_hostname:
122122
mine_function: grains.get
123123
key: id
@@ -210,7 +210,20 @@ To **include localhost** and local IP addresses (``127.0.0.1`` and ``::1``) use
210210

211211
openssh:
212212
known_hosts:
213-
include_localhost: True
213+
include_localhost: true
214+
215+
To prevent ever-changing IP addresses from being added to a host, use this::
216+
217+
openssh:
218+
known_hosts:
219+
omit_ip_address:
220+
- my.host.tld
221+
222+
To completely disable adding IP addresses::
223+
224+
openssh:
225+
known_hosts:
226+
omit_ip_address: true
214227

215228
``openssh.moduli``
216229
^^^^^^^^^^^^^^^^^^

openssh/files/default/ssh_known_hosts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
#}
44

55
{#- Generates one known_hosts entry per given key #}
6-
{%- macro known_host_entry(host, host_names, keys, include_localhost) %}
6+
{%- macro known_host_entry(host, host_names, keys, include_localhost, omit_ip_address) %}
77

88
{#- Get IPv4 and IPv6 addresses from the DNS #}
9-
{%- set ip4 = salt['dig.A'](host) -%}
10-
{%- set ip6 = salt['dig.AAAA'](host) -%}
9+
{%- if not (omit_ip_address is sameas true or host in omit_ip_address) %}
10+
{%- set ip4 = salt['dig.A'](host) -%}
11+
{%- set ip6 = salt['dig.AAAA'](host) -%}
12+
{%- else %}
13+
{%- set ip4 = [] -%}
14+
{%- set ip6 = [] -%}
15+
{%- endif %}
1116

1217
{#- The host names to use are to be found within the dict 'host_names'. #}
1318
{#- If there are none, the host is used directly. #}
@@ -59,6 +64,7 @@
5964
{%- set hostnames_target = salt['pillar.get']('openssh:known_hosts:hostnames:target', hostnames_target_default) -%}
6065
{%- set hostnames_tgt_type = salt['pillar.get']('openssh:known_hosts:hostnames:tgt_type', 'glob') -%}
6166
{%- set include_localhost = salt['pillar.get']('openssh:known_hosts:include_localhost', False) -%}
67+
{%- set omit_ip_address = salt['pillar.get']('openssh:known_hosts:omit_ip_address', []) -%}
6268

6369
{#- Lookup IP of all aliases so that when we have a matching IP, we inject the alias name
6470
in the SSH known_hosts entry -#}
@@ -98,5 +104,5 @@
98104

99105
{#- Loop over targetted minions -#}
100106
{%- for host, keys in host_keys| dictsort -%}
101-
{{ known_host_entry(host, host_names, keys, include_localhost) }}
107+
{{ known_host_entry(host, host_names, keys, include_localhost, omit_ip_address) }}
102108
{%- endfor -%}

pillar.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,13 @@ openssh:
335335
static:
336336
github.com: 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGm[...]'
337337
gitlab.com: 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCsj2bN[...]'
338+
# Prevent an ever-changing ssh_known_hosts file caused by a domain which
339+
# is served from multiple IP addresses.
340+
# To disable completely:
341+
# omit_ip_address: true
342+
# Or to disable by specific hosts:
343+
omit_ip_address:
344+
- github.com
338345

339346
# yamllint disable rule:line-length
340347
# specify DH parameters (see /etc/ssh/moduli)

test/salt/pillar/default.sls

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ openssh:
168168
static:
169169
github.com: 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGm[...]'
170170
gitlab.com: 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCsj2bN[...]'
171+
omit_ip_address:
172+
- github.com
171173

172174
# specify DH parameters (see /etc/ssh/moduli)
173175
# yamllint disable rule:line-length

0 commit comments

Comments
 (0)