+ file "/etc/netplan/99-chef.yaml" do
+ owner "root"
+ group "root"
+ mode "644"
+ content YAML.dump(netplan)
+ end
+elsif node[:networking][:engine] == "systemd-networkd"
+ file "/etc/netplan/99-chef.yaml" do
+ action :delete
+ end
+
+ package "netplan.io" do
+ action :purge
+ end
+
+ interfaces = node[:networking][:interfaces].collect do |name, interface|
+ [interface[:interface], name]
+ end.to_h
+
+ node[:networking][:interfaces].each do |name, interface|
+ if interface[:interface] =~ /^(.*)\.(\d+)$/
+ vlan_interface = Regexp.last_match(1)
+ vlan_id = Regexp.last_match(2)
+
+ parent = interfaces[vlan_interface] || "vlans_#{vlan_interface}"
+
+ node.default_unless[:networking][:interfaces][parent][:interface] = vlan_interface,
+ node.default_unless[:networking][:interfaces][parent][:vlans] = []
+
+ node.default[:networking][:interfaces][parent][:vlans] << vlan_id
+ end
+
+ next unless interface[:role] && (role = node[:networking][:roles][interface[:role]])
+
+ if interface[:inet] && role[:inet]
+ node.default[:networking][:interfaces][name][:inet][:prefix] = role[:inet][:prefix]
+ node.default[:networking][:interfaces][name][:inet][:gateway] = role[:inet][:gateway]
+ node.default[:networking][:interfaces][name][:inet][:routes] = role[:inet][:routes]
+ end
+
+ if interface[:inet6] && role[:inet6]
+ node.default[:networking][:interfaces][name][:inet6][:prefix] = role[:inet6][:prefix]
+ node.default[:networking][:interfaces][name][:inet6][:gateway] = role[:inet6][:gateway]
+ node.default[:networking][:interfaces][name][:inet6][:routes] = role[:inet6][:routes]
+ end
+
+ node.default[:networking][:interfaces][name][:metric] = role[:metric]
+ node.default[:networking][:interfaces][name][:zone] = role[:zone]
+ end
+
+ node[:networking][:interfaces].each do |_, interface|
+ file "/run/systemd/network/10-netplan-#{interface[:interface]}.netdev" do
+ action :delete
+ end
+
+ if interface[:interface] =~ /^.*\.(\d+)$/
+ template "/etc/systemd/network/10-#{interface[:interface]}.netdev" do
+ source "vlan.netdev.erb"
+ owner "root"
+ group "root"
+ mode "644"
+ variables :interface => interface, :vlan => Regexp.last_match(1)
+ notifies :run, "execute[networkctl-delete-#{interface[:interface]}]"
+ notifies :run, "notify_group[networkctl-reload]"
+ end
+
+ execute "networkctl-delete-#{interface[:interface]}" do
+ action :nothing
+ command "networkctl delete #{interface[:interface]}"
+ only_if { ::File.exist?("/sys/class/net/#{interface[:interface]}") }
+ end
+ elsif interface[:interface] =~ /^bond\d+$/
+ template "/etc/systemd/network/10-#{interface[:interface]}.netdev" do
+ source "bond.netdev.erb"
+ owner "root"
+ group "root"
+ mode "644"
+ variables :interface => interface
+ notifies :run, "execute[networkctl-delete-#{interface[:interface]}]"
+ notifies :run, "notify_group[networkctl-reload]"
+ end
+
+ execute "networkctl-delete-#{interface[:interface]}" do
+ action :nothing
+ command "networkctl delete #{interface[:interface]}"
+ only_if { ::File.exist?("/sys/class/net/#{interface[:interface]}") }
+ end
+
+ interface[:bond][:slaves].each do |slave|
+ file "/run/systemd/network/10-netplan-#{slave}.network" do
+ action :delete
+ end
+
+ template "/etc/systemd/network/10-#{slave}.network" do
+ source "slave.network.erb"
+ owner "root"
+ group "root"
+ mode "644"
+ variables :master => interface, :slave => slave
+ notifies :run, "notify_group[networkctl-reload]"
+ end
+ end
+ end
+
+ file "/run/systemd/network/10-netplan-#{interface[:interface]}.network" do
+ action :delete
+ end
+
+ template "/etc/systemd/network/10-#{interface[:interface]}.network" do
+ source "network.erb"
+ owner "root"
+ group "root"
+ mode "644"
+ variables :interface => interface
+ notifies :run, "notify_group[networkctl-reload]"
+ end
+ end
+
+ service "systemd-networkd" do
+ action [:enable, :start]
+ end