diff --git a/cloudinit/net/network_manager.py b/cloudinit/net/network_manager.py index 74850a79f8c..7d7772e1102 100644 --- a/cloudinit/net/network_manager.py +++ b/cloudinit/net/network_manager.py @@ -246,7 +246,7 @@ def mac_addr(addr): """ return addr.replace("-", ":").upper() - def render_interface(self, iface, renderer): + def render_interface(self, iface, network_state, renderer): """ Integrate information from network state interface information into the connection. Most of the work is done here. @@ -307,6 +307,7 @@ def render_interface(self, iface, renderer): device_mtu = iface["mtu"] ipv4_mtu = None + has_address = False found_nameservers = [] found_dns_search = [] @@ -317,6 +318,7 @@ def render_interface(self, iface, renderer): self._set_ip_method(family, subnet["type"]) if "address" in subnet: self._add_address(family, subnet) + has_address = True if "gateway" in subnet: self.config[family]["gateway"] = subnet["gateway"] for route in subnet["routes"]: @@ -334,6 +336,14 @@ def render_interface(self, iface, renderer): found_nameservers = iface["dns"]["nameservers"] found_dns_search = iface["dns"]["search"] + # Now add in the global DNS information if we have an address but no + # DNS information yet + if has_address: + if not found_nameservers and network_state.dns_nameservers: + found_nameservers = network_state.dns_nameservers + if not found_dns_search and network_state.dns_searchdomains: + found_dns_search = network_state.dns_searchdomains + for nameserver in found_nameservers: self._add_nameserver(nameserver) if found_dns_search: @@ -453,7 +463,7 @@ def render_network_state( # Now render the actual interface configuration for iface in network_state.iter_interfaces(): conn = self.connections[iface["name"]] - conn.render_interface(iface, self) + conn.render_interface(iface, network_state, self) # And finally write the files for con_id, conn in self.connections.items(): diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py index 742982dce88..fe0d27ea0c2 100644 --- a/tests/unittests/test_net.py +++ b/tests/unittests/test_net.py @@ -646,6 +646,7 @@ may-fail=false address1=172.19.1.34/22 route1=0.0.0.0/0,172.19.3.254 +dns=172.19.0.12; """.lstrip(), ), @@ -2822,6 +2823,8 @@ method=manual may-fail=false address1=192.168.200.7/24 + dns=8.8.8.8;4.4.4.4;8.8.4.4; + dns-search=barley.maas;wark.maas;foobar.maas; """ ), @@ -2930,12 +2933,15 @@ method=manual may-fail=false address1=192.168.14.2/24 + dns=8.8.8.8;4.4.4.4;8.8.4.4; + dns-search=barley.maas;wark.maas;foobar.maas; [ipv6] method=manual may-fail=false address1=2001:1::1/64 route1=::/0,2001:4800:78ff:1b::1 + dns-search=barley.maas;wark.maas;foobar.maas; """ ),