Add ability to clear DNS entries on static DNS

Execution Module:
- Passing no DNS servers will clear the list.
- Update documentation
- Added a note about clearing DNS entries

State Module
- An empty list in dns_servers will clear the entires
- None in dns_servers will do nothing (default behavior)
- Update Documentation
This commit is contained in:
twangboy 2018-09-24 15:28:54 -06:00
parent d5a75bf9d0
commit 048561e56f
No known key found for this signature in database
GPG Key ID: 93FF3BDEB278C9EB
2 changed files with 79 additions and 25 deletions

View File

@ -319,6 +319,19 @@ def set_static_dns(iface, *addrs):
'''
Set static DNS configuration on a Windows NIC
Args:
iface (str): The name of the interface to set
addrs (*):
One or more DNS servers to be added
.. note::
If no DNS servers are passed the list will be cleared.
Returns:
dict: A dictionary containing the new DNS settings
CLI Example:
.. code-block:: bash
@ -326,6 +339,15 @@ def set_static_dns(iface, *addrs):
salt -G 'os_family:Windows' ip.set_static_dns 'Local Area Connection' '192.168.1.1'
salt -G 'os_family:Windows' ip.set_static_dns 'Local Area Connection' '192.168.1.252' '192.168.1.253'
'''
# Clear the list of DNS servers if not passed
if not addrs:
log.debug('Clearing list of DNS servers')
cmd = ['netsh', 'int', 'ip', 'set', 'dns',
'name={0}'.format(iface),
'source=static',
'address=none']
__salt__['cmd.run'](cmd, python_shell=False)
return {'Interface': iface, 'DNS Server': []}
addr_index = 1
for addr in addrs:
if addr_index == 1:

View File

@ -103,10 +103,8 @@ def _validate(dns_proto, dns_servers, ip_proto, ip_addrs, gateway):
'set to \'static\'.'
)
else:
if not dns_servers:
errors.append(
'The dns_servers param is required to set static DNS servers.'
)
if dns_servers is None:
pass
elif not isinstance(dns_servers, list):
errors.append(
'The dns_servers param must be formatted as a list.'
@ -214,31 +212,60 @@ def managed(name,
'''
Ensure that the named interface is configured properly.
name
Args:
name (str):
The name of the interface to manage
dns_proto : None
dns_proto (str): None
Set to ``static`` and use the ``dns_servers`` parameter to provide a
list of DNS nameservers. set to ``dhcp`` to use DHCP to get the DNS
servers.
dns_servers : None
A list of static DNS servers.
dns_servers (list): None
A list of static DNS servers. To clear the list of DNS servers pass
an empty list. ``[]``
ip_proto : None
ip_proto (str): None
Set to ``static`` and use the ``ip_addrs`` and (optionally) ``gateway``
parameters to provide a list of static IP addresses and the default
gateway. Set to ``dhcp`` to use DHCP.
ip_addrs : None
A list of static IP addresses.
ip_addrs (list): None
A list of static IP addresses with netmask flag, ie: 192.168.0.11/24
gateway : None
A list of static IP addresses.
gateway (str): None
The gateway to set for the interface
enabled : True
enabled (bool): True
Set to ``False`` to ensure that this interface is disabled.
Returns:
dict: A dictionary of old and new settings
Example:
.. code-block:: yaml
Ethernet1:
network.managed:
- dns_proto: static
- dns_servers:
- 8.8.8.8
- 8.8.8.4
- ip_proto: static
- ip_addrs:
- 192.168.0.100/24
Clear DNS entries example:
.. code-block:: yaml
Ethernet1:
network.managed:
- dns_proto: static
- dns_servers: []
- ip_proto: dhcp
'''
ret = {
'name': name,
@ -317,6 +344,12 @@ def managed(name,
ip_proto,
ip_addrs,
gateway)
# If dns_servers is the default `None` make no changes
# To clear the list, pass an empty dict
if dns_servers is None:
changes.pop('dns_servers')
if not changes:
return ret
@ -357,8 +390,7 @@ def managed(name,
if changes.get('dns_proto') == 'dhcp':
__salt__['ip.set_dhcp_dns'](name)
elif changes.get('dns_servers'):
if changes.get('dns_servers'):
elif 'dns_servers' in changes:
__salt__['ip.set_static_dns'](name, *changes['dns_servers'])
if changes.get('ip_proto') == 'dhcp':