Skip to content

Instantly share code, notes, and snippets.

@kjetilho
Created August 6, 2024 22:49
Show Gist options
  • Save kjetilho/20b4186bac4c5d2c9edc1f3bc6df9aab to your computer and use it in GitHub Desktop.
Save kjetilho/20b4186bac4c5d2c9edc1f3bc6df9aab to your computer and use it in GitHub Desktop.
routine network configuration
class networkd(
Hash $bonds = {},
Optional[Hash] $networks = undef,
Hash $units = {},
)
{
package {
# Ubuntu
'netplan.io': ensure => absent;
# RHEL
'netplan': ensure => absent;
}
# The $bonds hash only handles the simple case. For advanced
# bonding, use $interfaces and $units. Note the bondX.network unit
# itself must be configured via $networks.
$bonds.each |$bond, $members| {
systemd::network {
"${bond}.netdev":
content => @("NETDEV"),
# Managed by Puppet
[NetDev]
Name=${bond}
Kind=bond
[Bond]
Mode=802.3ad
LACPTransmitRate=fast
| NETDEV
;
}
$members.each |$member| {
systemd::network {
"${member}.network":
content => @("MEMBER"),
# Managed by Puppet
[Match]
Name=${member}
[Network]
Bond=${bond}
LinkLocalAddressing=no
EmitLLDP=yes
| MEMBER
;
}
}
}
$cidr = extlib::netmask_to_cidr($facts['networking']['netmask'])
$ip4_cidr = "${facts['networking']['ip']}/${cidr}"
$ip4_gateway = inline_template('<%= @facts["networking"]["network"].sub(%r{(\d+)$}) { |n| n.to_i+1 } %>')
if $facts['networking']['ip6'] {
$default_config = @("DEFAULT_IPV6")
[Link]
RequiredFamilyForOnline=ipv6
RequiredForOnline=routable
[Network]
Address=${ip4_cidr}
LLDP=yes
ConfigureWithoutCarrier=yes
IPv6AcceptRA=true
[Route]
Destination=0.0.0.0/0
Gateway=${ip4_gateway}
| DEFAULT_IPV6
} else {
$default_config = @("DEFAULT_IPV4")
[Link]
RequiredFamilyForOnline=ipv4
RequiredForOnline=routable
[Network]
Address=${ip4_cidr}
LLDP=yes
ConfigureWithoutCarrier=yes
IPv6AcceptRA=false
[Route]
Destination=0.0.0.0/0
Gateway=${ip4_gateway}
| DEFAULT_IPV4
}
$default_network = { $facts['networking']['primary'] => $default_config }
$networks.lest || { $default_network }.each |$iface, $config| {
if $config == 'default' {
$_config = $default_config
} else {
$_config = $config
}
systemd::network {
"${iface}.network":
content => "# Managed by Puppet\n[Match]\nName=${iface}\n\n${_config}"
}
}
$units.each |$unitname, $config| {
systemd::network {
$unitname:
content => "# Managed by Puppet\n${config}"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment