Remove non-matching IPs from hosts file

This commit is contained in:
Erik Johnson 2018-11-27 12:07:01 -06:00
parent ec297a0944
commit 30f1b85c09
No known key found for this signature in database
GPG key ID: 5E5583C437808F3F

View file

@ -75,36 +75,78 @@ def present(name, ip): # pylint: disable=C0103
The host to assign an ip to
ip
The ip addr(s) to apply to the host
The ip addr(s) to apply to the host. Can be a single IP or a list of IP
addresses.
'''
ret = {'name': name,
'changes': {},
'result': None,
'result': None if __opts__['test'] else True,
'comment': ''}
if not isinstance(ip, list):
ip = [ip]
all_hosts = __salt__['hosts.list_hosts']()
comments = []
for _ip in ip:
if __salt__['hosts.has_pair'](_ip, name):
ret['result'] = True
comments.append('Host {0} ({1}) already present'.format(name, _ip))
to_add = set()
to_remove = set()
# First check for IPs not currently in the hosts file
to_add.update([(addr, name) for addr in ip if addr not in all_hosts])
# Now sweep through the hosts file and look for entries matching either the
# IP address(es) or hostname.
for addr, aliases in six.iteritems(all_hosts):
if addr not in ip:
if name in aliases:
# Found match for hostname, but the corresponding IP is not in
# our list, so we need to remove it.
to_remove.add((addr, name))
else:
if __opts__['test']:
comments.append('Host {0} ({1}) needs to be added/updated'.format(name, _ip))
if name in aliases:
# No changes needed for this IP address and hostname
comments.append(
'Host {0} ({1}) already present'.format(name, addr)
)
else:
if salt.utils.validate.net.ipv4_addr(_ip) or salt.utils.validate.net.ipv6_addr(_ip):
if __salt__['hosts.add_host'](_ip, name):
ret['changes'] = {'host': name}
ret['result'] = True
comments.append('Added host {0} ({1})'.format(name, _ip))
else:
ret['result'] = False
comments.append('Failed to set host')
# IP address listed in hosts file, but hostname is not present.
# We will need to add it.
if salt.utils.validate.net.ip_addr(addr):
to_add.add((addr, name))
else:
ret['result'] = False
comments.append('Invalid IP Address for {0} ({1})'.format(name, _ip))
comments.append(
'Invalid IP Address for {0} ({1})'.format(name, addr)
)
for addr, name in to_add:
if __opts__['test']:
comments.append(
'Host {0} ({1}) would be added'.format(name, addr)
)
else:
if __salt__['hosts.add_host'](addr, name):
comments.append('Added host {0} ({1})'.format(name, addr))
else:
ret['result'] = False
comments.append('Failed to add host {0} ({1})'.format(name, addr))
continue
ret['changes'].setdefault('added', {}).setdefault(addr, []).append(name)
for addr, name in to_remove:
if __opts__['test']:
comments.append(
'Host {0} ({1}) would be removed'.format(name, addr)
)
else:
if __salt__['hosts.rm_host'](addr, name):
comments.append('Removed host {0} ({1})'.format(name, addr))
else:
ret['result'] = False
comments.append('Failed to remove host {0} ({1})'.format(name, addr))
continue
ret['changes'].setdefault('removed', {}).setdefault(addr, []).append(name)
ret['comment'] = '\n'.join(comments)
return ret