Skip to content

Commit 3d10af0

Browse files
liangwen12yearrichm
authored andcommitted
Support the DNS priority
The users want to configure the priority of DNS servers, add support for that. Fixes #505. Signed-off-by: Wen Liang <liangwen12year@gmail.com>
1 parent 5320860 commit 3d10af0

File tree

7 files changed

+84
-12
lines changed

7 files changed

+84
-12
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,15 @@ The IP configuration supports the following options:
514514
When using a caching DNS plugin (dnsmasq or systemd-resolved in NetworkManager.conf)
515515
then "edns0" and "trust-ad" are automatically added.
516516

517+
- `dns_priority`
518+
519+
DNS servers priority. The relative priority for DNS servers specified by this
520+
setting. The default value is 0, a lower numerical value has higher priority.
521+
The valid value of `dns_priority` ranges from -2147483648 to 2147483647. Negative
522+
values have the special effect of excluding other configurations with a greater
523+
numerical priority value; so in presence of at least one negative priority, only
524+
DNS servers from connections with the lowest priority value will be used.
525+
517526
- `gateway4` and `gateway6`
518527

519528
The default gateway for IPv4 (`gateway4`) or IPv6 (`gateway6`) packets.

examples/eth_dns_support.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
route_metric4: 100
1010
dhcp4: no
1111
gateway4: 192.0.2.1
12+
dns_priority: 9999
1213
dns:
1314
- 192.0.2.2
1415
- 198.51.100.5

library/network_connections.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,10 @@ def connection_create(self, connections, idx, connection_current=None):
10921092
s_ip4.clear_dns_options(False)
10931093
for option in ip["dns_options"]:
10941094
s_ip4.add_dns_option(option)
1095+
if ip["dns_priority"] is not None:
1096+
s_ip4.set_property(
1097+
NM.SETTING_IP_CONFIG_DNS_PRIORITY, ip["dns_priority"]
1098+
)
10951099

10961100
is_ipv6_configured = False
10971101
if ip["ipv6_disabled"]:
@@ -1144,6 +1148,10 @@ def connection_create(self, connections, idx, connection_current=None):
11441148
s_ip6.clear_dns_options(False)
11451149
for option in ip["dns_options"]:
11461150
s_ip6.add_dns_option(option)
1151+
if ip["dns_priority"] is not None:
1152+
s_ip6.set_property(
1153+
NM.SETTING_IP_CONFIG_DNS_PRIORITY, ip["dns_priority"]
1154+
)
11471155

11481156
if ip["route_append_only"] and connection_current:
11491157
for r in self.setting_ip_config_get_routes(

module_utils/network_lsr/argument_validator.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,12 @@ def __init__(self):
887887
),
888888
default_value=list,
889889
),
890+
ArgValidatorNum(
891+
"dns_priority",
892+
val_min=-2147483648,
893+
val_max=2147483647,
894+
default_value=0,
895+
),
890896
ArgValidatorList(
891897
"routing_rule",
892898
nested=ArgValidatorIPRoutingRule("routing_rule[?]"),
@@ -911,6 +917,7 @@ def __init__(self):
911917
"dns": [],
912918
"dns_search": [],
913919
"dns_options": [],
920+
"dns_priority": 0,
914921
},
915922
)
916923

@@ -2470,19 +2477,23 @@ def _ipv6_is_not_configured(connection):
24702477
"IPv6 needs to be enabled to support IPv6 nameservers.",
24712478
)
24722479
# when IPv4 and IPv6 are disabled, setting ip.dns_options or
2473-
# ip.dns_search is not allowed
2474-
if connection["ip"]["dns_search"] or connection["ip"]["dns_options"]:
2480+
# ip.dns_search or ip.dns_priority is not allowed
2481+
if (
2482+
connection["ip"]["dns_search"]
2483+
or connection["ip"]["dns_options"]
2484+
or connection["ip"]["dns_priority"]
2485+
):
24752486
if not _ipv4_enabled(connection) and connection["ip"]["ipv6_disabled"]:
24762487
raise ValidationError.from_connection(
24772488
idx,
2478-
"Setting 'dns_search' or 'dns_options' is not allowed when "
2479-
"both IPv4 and IPv6 are disabled.",
2489+
"Setting 'dns_search', 'dns_options' and 'dns_priority' are not "
2490+
"allowed when both IPv4 and IPv6 are disabled.",
24802491
)
24812492
elif not _ipv4_enabled(connection) and _ipv6_is_not_configured(connection):
24822493
raise ValidationError.from_connection(
24832494
idx,
2484-
"Setting 'dns_search' or 'dns_options' is not allowed when "
2485-
"IPv4 is disabled and IPv6 is not configured.",
2495+
"Setting 'dns_search', 'dns_options' and 'dns_priority' are not "
2496+
"allowed when IPv4 is disabled and IPv6 is not configured.",
24862497
)
24872498
# DNS options 'inet6', 'ip6-bytestring', 'ip6-dotint', 'no-ip6-dotint' are only
24882499
# supported for IPv6 configuration, so raise errors when IPv6 is disabled

tests/playbooks/tests_eth_dns_support.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
route_metric4: 100
3535
dhcp4: no
3636
gateway4: 192.0.2.1
37+
dns_priority: 9999
3738
dns:
3839
- 192.0.2.2
3940
- 198.51.100.5
@@ -108,6 +109,13 @@
108109
- "'timeout:1' in ipv6_dns.stdout"
109110
msg: "DNS options are configured incorrectly"
110111

112+
- name: "Assert that DNS priority is configured correctly"
113+
assert:
114+
that:
115+
- "'9999' in ipv4_dns.stdout"
116+
- "'9999' in ipv6_dns.stdout"
117+
msg: "DNS priority is configured incorrectly"
118+
111119
- import_playbook: down_profile+delete_interface.yml
112120
vars:
113121
profile: "{{ interface }}"

tests/playbooks/tests_ipv6_dns_search.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@
138138
assert:
139139
that:
140140
- __network_connections_result.stderr is search("Setting
141-
'dns_search' or 'dns_options' is not allowed when both IPv4 and
142-
IPv6 are disabled.")
141+
'dns_search', 'dns_options' and 'dns_priority' are not allowed
142+
when both IPv4 and IPv6 are disabled.")
143143
msg: Reconfiguring network connection is not failed with the error
144-
"Setting 'dns_search' or 'dns_options' is not allowed when both
145-
IPv4 and IPv6 are disabled."
144+
"Setting 'dns_search', 'dns_options', and 'dns_priority' are not
145+
allowed when both IPv4 and IPv6 are disabled."
146146
when: ansible_distribution_major_version | int > 7
147147

148148
always:

0 commit comments

Comments
 (0)