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 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: CLI Example:
.. code-block:: bash .. 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.1'
salt -G 'os_family:Windows' ip.set_static_dns 'Local Area Connection' '192.168.1.252' '192.168.1.253' 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 addr_index = 1
for addr in addrs: for addr in addrs:
if addr_index == 1: if addr_index == 1:

View File

@ -103,10 +103,8 @@ def _validate(dns_proto, dns_servers, ip_proto, ip_addrs, gateway):
'set to \'static\'.' 'set to \'static\'.'
) )
else: else:
if not dns_servers: if dns_servers is None:
errors.append( pass
'The dns_servers param is required to set static DNS servers.'
)
elif not isinstance(dns_servers, list): elif not isinstance(dns_servers, list):
errors.append( errors.append(
'The dns_servers param must be formatted as a list.' '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. Ensure that the named interface is configured properly.
name Args:
The name of the interface to manage
dns_proto : None name (str):
Set to ``static`` and use the ``dns_servers`` parameter to provide a The name of the interface to manage
list of DNS nameservers. set to ``dhcp`` to use DHCP to get the DNS
servers.
dns_servers : None dns_proto (str): None
A list of static DNS servers. 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.
ip_proto : None dns_servers (list): None
Set to ``static`` and use the ``ip_addrs`` and (optionally) ``gateway`` A list of static DNS servers. To clear the list of DNS servers pass
parameters to provide a list of static IP addresses and the default an empty list. ``[]``
gateway. Set to ``dhcp`` to use DHCP.
ip_addrs : None ip_proto (str): None
A list of static IP addresses. 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.
gateway : None ip_addrs (list): None
A list of static IP addresses. A list of static IP addresses with netmask flag, ie: 192.168.0.11/24
enabled : True gateway (str): None
Set to ``False`` to ensure that this interface is disabled. The gateway to set for the interface
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 = { ret = {
'name': name, 'name': name,
@ -317,6 +344,12 @@ def managed(name,
ip_proto, ip_proto,
ip_addrs, ip_addrs,
gateway) 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: if not changes:
return ret return ret
@ -357,9 +390,8 @@ def managed(name,
if changes.get('dns_proto') == 'dhcp': if changes.get('dns_proto') == 'dhcp':
__salt__['ip.set_dhcp_dns'](name) __salt__['ip.set_dhcp_dns'](name)
elif changes.get('dns_servers'): elif 'dns_servers' in changes:
if changes.get('dns_servers'): __salt__['ip.set_static_dns'](name, *changes['dns_servers'])
__salt__['ip.set_static_dns'](name, *changes['dns_servers'])
if changes.get('ip_proto') == 'dhcp': if changes.get('ip_proto') == 'dhcp':
__salt__['ip.set_dhcp_ip'](name) __salt__['ip.set_dhcp_ip'](name)