Defined Type: psick::network::interface

Defined in:
manifests/network/interface.pp

Summary

A define to manage network interfaces

Overview

This define manages network interfaces on different operating systems. It provides some default configurations that can be overridden via relevant parameters.

Examples:

Configure an interface to use DHCP

psick::network::interface { 'eth0':
  enable_dhcp                => true,
}

Configure an interface with a given IP address

psick::network::interface { 'eth0':
  ipv4_address => 10.42.42.42,
  ipv4_netmask => 255.255.255.0,
}

Parameters:

  • ensure (Enum['present','absent']) (defaults to: 'present')

    If to create or remove the relevant configuration file.

  • template (String) (defaults to: "psick/network/interface/${facts['os']['family']}.epp")

    The epp or erb template to use for the interface configuration file. Default is automatically defined based on $::os,

  • config_path (Optional[String]) (defaults to: undef)

    The path of the interface configuration file. Default is automatically defined based on the Operating System.

  • enable_dhcp

    If to configure the interface to use dhcp.

  • interface (String) (defaults to: $title)

    The name of the interface to use. Default value is the $title of the define. Can be set explicitly in case different title names have to used.

  • description (String) (defaults to: "Interface ${title}")

    A free text description to use, where applicable, to describe the interface. It has no real effect on the interface configuration.

  • ipv4_address (Optional[Stdlib::IP::Address::V4]) (defaults to: undef)

    The optional IPv4 address of the interface.

  • ipv4_netmask (Optional[Stdlib::IP::Address::V4]) (defaults to: undef)

    The optional netmask of the IPv4 address.

  • ipv4_network (Optional[Stdlib::IP::Address::V4]) (defaults to: undef)

    The optional IPv4 network address.

  • ipv4_broadcast (Optional[Stdlib::IP::Address::V4]) (defaults to: undef)

    The optional IPv4 broadcast address.

  • ipv6_address (Optional[Stdlib::IP::Address::V6]) (defaults to: undef)

    The optional IPv6 address of the interface.

  • ipv6_netmask (Optional[Stdlib::IP::Address::V6]) (defaults to: undef)

    The optional netmask of the IPv6 address.

  • ipv6_network (Optional[Stdlib::IP::Address::V6]) (defaults to: undef)

    The optional IPv6 network address.

  • mtu

    The interface Maximum Transmission Unit (in bytes).

  • mac (Optional[Integer]) (defaults to: undef)

    The (optional) interface MAC address.

  • redhat_extra_settings (Hash) (defaults to: {})

    A free hash of custom settings to add to the interface configuration. Used only on redhat family nodes.

  • redhat_extra_header (Optional[String]) (defaults to: undef)

    A custom free text to add as header to the interface configuration file on RedHat family nodes.

  • redhat_extra_footer (Optional[String]) (defaults to: undef)

    A custom free text to add as footer to the interface configuration file on RedHat family nodes.

  • debian_extra_settings (Hash) (defaults to: {})

    Equivalent of redhat_extra_settings for Debian osfamily.

  • debian_extra_header (Optional[String]) (defaults to: undef)

    Equivalent of redhat_extra_header for Debian osfamily.

  • debian_extra_footer (Optional[String]) (defaults to: undef)

    Equivalent of redhat_extra_footer for Debian osfamily.

  • suse_extra_settings (Hash) (defaults to: {})

    Equivalent of redhat_extra_settings for Suse osfamily.

  • suse_extra_header (Optional[String]) (defaults to: undef)

    Equivalent of redhat_extra_header for Suse osfamily.

  • suse_extra_footer (Optional[String]) (defaults to: undef)

    Equivalent of redhat_extra_footer for Suse osfamily.

  • solaris_extra_settings (Hash) (defaults to: {})

    Equivalent of redhat_extra_settings for Solaris.

  • solaris_extra_header (Optional[String]) (defaults to: undef)

    Equivalent of redhat_extra_header for Solaris.

  • solaris_extra_footer (Optional[String]) (defaults to: undef)

    Equivalent of redhat_extra_footer for Solaris.

  • use_default_settings (Boolean) (defaults to: true)

    If to use some default settings also based on $os_features to correctly configure interface files. They can be overridden via the osfamily extra_settings.

  • os_features (Array) (defaults to: ['check_link_down','auto'])

    Some features which affect the default_settings.

  • config_file_notify (Variant[Undef,Resource,String]) (defaults to: 'class_default')

    The Resource to trigger when a configuration change occurs. Default is what is se in $::psick::network::config_file_notify

  • manage_prerequisites (Boolean) (defaults to: true)

    If to automatically manage prerequisite resources like packages when needed by the interface type

  • enable (Boolean) (defaults to: true)
  • use_netplan (Boolean) (defaults to: lookup('psick::network::use_netplan',Boolean,first,false))
  • ipv4_dhcp (Boolean) (defaults to: false)
  • ipv4_gateway (Optional[Stdlib::IP::Address::V4]) (defaults to: undef)
  • ipv4_mtu (Optional[Integer]) (defaults to: undef)
  • ipv6_dhcp (Boolean) (defaults to: false)
  • ipv6_gateway (Optional[Stdlib::IP::Address::V6]) (defaults to: undef)
  • ipv6_mtu (Optional[Integer]) (defaults to: undef)
  • mac_override (Boolean) (defaults to: false)
  • config_file_per_interface (Boolean) (defaults to: true)
  • suppress_warnings (Boolean) (defaults to: false)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'manifests/network/interface.pp', line 62

define psick::network::interface (
  Enum['present','absent'] $ensure                  = 'present',
  Boolean $enable                                   = true,
  Boolean $use_netplan                              = lookup('psick::network::use_netplan',Boolean,first,false), # lint:ignore:lookup_in_parameter

  String $template                                  = "psick/network/interface/${facts['os']['family']}.epp",
  Optional[String] $config_path                     = undef,

  String $interface                                 = $title,
  String $description                               = "Interface ${title}",

  Boolean                           $ipv4_dhcp      = false,
  Optional[Stdlib::IP::Address::V4] $ipv4_address   = undef,
  Optional[Stdlib::IP::Address::V4] $ipv4_netmask   = undef,
  Optional[Stdlib::IP::Address::V4] $ipv4_network   = undef,
  Optional[Stdlib::IP::Address::V4] $ipv4_broadcast = undef,
  Optional[Stdlib::IP::Address::V4] $ipv4_gateway   = undef,
  Optional[Integer]                 $ipv4_mtu       = undef,

  Boolean                           $ipv6_dhcp      = false,
  Optional[Stdlib::IP::Address::V6] $ipv6_address   = undef,
  Optional[Stdlib::IP::Address::V6] $ipv6_netmask   = undef,
  Optional[Stdlib::IP::Address::V6] $ipv6_network   = undef,
  Optional[Stdlib::IP::Address::V6] $ipv6_gateway   = undef,
  Optional[Integer]                 $ipv6_mtu       = undef,

  Optional[Integer] $mac                            = undef,
  Boolean $mac_override                             = false,

  Hash $redhat_extra_settings                       = {},
  Optional[String] $redhat_extra_header             = undef,
  Optional[String] $redhat_extra_footer             = undef,

  Hash $debian_extra_settings                       = {},
  Optional[String] $debian_extra_header             = undef,
  Optional[String] $debian_extra_footer             = undef,

  Hash $suse_extra_settings                         = {},
  Optional[String] $suse_extra_header               = undef,
  Optional[String] $suse_extra_footer               = undef,

  Hash $solaris_extra_settings                      = {},
  Optional[String] $solaris_extra_header            = undef,
  Optional[String] $solaris_extra_footer            = undef,

  Boolean $use_default_settings                     = true,

  Array $os_features                                = ['check_link_down','auto'],

  Variant[Undef,Resource,String] $config_file_notify = 'class_default',
  Boolean $config_file_per_interface                = true,

  Boolean $manage_prerequisites                     = true,
  Boolean $suppress_warnings                        = false,
) {
  case $facts['os']['family'] {
    'RedHat': {
      if 'check_link_down' in $os_features {
        $os_footer = @("EOF")
          check_link_down() {
            return 1;
          }
          |- EOF
      } else {
        $os_footer = ''
      }
      $os_header = ''
      $os_settings = {
        'ONBOOT'    => $enable ? {
          true  => 'yes',
          false => 'yes',
        },
        'BOOTPROTO' => $ipv4_dhcp ? {
          true  => 'dhcp',
          false => 'none',
        },
        'DEVICE'    => $interface,
        'IPADDR'    => $ipv4_address,
        'NETWORK'   => $ipv4_network,
        'NETMASK'   => $ipv4_netmask,
        'BROADCAST' => $ipv4_broadcast,
        'GATEWAY'   => $ipv4_gateway,
        'MTU'       => $ipv4_mtu,
        'HWADDR'    => $mac_override ? {
          true    => undef,
          default => $mac,
        },
        'MACADDR'   => $mac_override ? {
          true    => $mac,
          default => undef,
        },
        'DHCPV6C'   => $ipv6_dhcp ? {
          true  => 'yes',
          false => undef,
        },
        'IPV6ADDR'  => $ipv6_address,
        'IPV6MTU'   => $ipv6_mtu,
        'IPV6INIT'  => $ipv6_dhcp ? {
          true  => 'yes',
          false => $ipv6_address ? {
            undef   => undef,
            default => 'yes',
          },
        },
      }
      $extra_settings = $redhat_extra_settings
      $extra_header = $redhat_extra_header
      $extra_footer = $redhat_extra_footer
    }
    'Debian': {
      $debian_method = $ipv4_dhcp ? {
        true  => 'dhcp',
        false => 'static',
      }
      $os_header = "iface ${interface} inet ${debian_method}\n"
      $os_footer = ''
      $os_settings = {
        address => $ipv4_address,
        netmask => $ipv4_netmask,
      }
      $extra_settings = $debian_extra_settings
      $extra_header = $debian_extra_header
      $extra_footer = $debian_extra_footer
    }
    'SuSE': {
      $os_header = ''
      $os_footer = ''
      $os_settings = {
        'STARTMODE' => $enable ? {
          true  => 'auto',
          false => 'off',
        },
        'BOOTPROTO' => $ipv4_dhcp ? {
          true  => 'dhcp',
          false => 'static',
        },
        'DEVICE'    => $interface,
        'IPADDR'    => $ipv4_address,
        'NETWORK'   => $ipv4_network,
        'NETMASK'   => $ipv4_netmask,
        'BROADCAST' => $ipv4_broadcast,
        'GATEWAY'   => $ipv4_gateway,
        'MTU'       => $ipv4_mtu,
        'LLADDR'    => $mac,
      }
      $extra_settings = $suse_extra_settings
      $extra_header = $suse_extra_header
      $extra_footer = $suse_extra_footer
    }
    'Solaris': {
      $os_header = ''
      $os_footer = ''
      $os_settings = {}
      $extra_settings = $solaris_extra_settings
      $extra_header = $solaris_extra_header
      $extra_footer = $solaris_extra_footer
    }
    default: {}
  }

  # $settings variable is used in templates
  if $use_default_settings {
    $settings = delete_undef_values($os_settings + $extra_settings)
    $header = "${os_header}${extra_header}"
    $footer = "${os_footer}${extra_footer}"
  } else {
    $settings = delete_undef_values($extra_settings)
    $header = $extra_header
    $footer = $extra_footer
  }

  $params = {
    settings    => $settings,
    header      => $header,
    footer      => $footer,
    interface   => $interface,
    description => $description,
  }

  # Content used in interface configuration file
  $template_type=$template[-4,4]
  case $template_type {
    '.epp': {
      $content = epp($template, { params => $params })
    }
    '.erb': {
      $content = template($template)
    }
    default: {
      # If no known extension is present, we treat $template as an erb template
      $content = template($template)
    }
  }
  # Configuration file path
  case $facts['os']['family'] {
    'RedHat': {
      $config_file_path = pick($config_path,"/etc/sysconfig/network-scripts/ifcfg-${title}")
    }
    'Suse': {
      $config_file_path = pick($config_path,"/etc/sysconfig/network/ifcfg-${title}")
    }
    'Debian': {
      if $facts['os']['name'] == 'CumulusLinux' {
        $config_file_path = pick($config_path,"/etc/network/interfaces.d/${title}")
      } else {
        $config_file_path = pick($config_path,"/etc/network/interfaces.d/${title}.cfg")
      }
    }
    'Solaris': {
      $config_file_path = pick($config_path,"/etc/hostname.${title}")
    }
    default: {}
  }

  # Define how to restart network service
  $real_config_file_notify = $config_file_notify ? {
    'class_default' => $psick::network::manage_config_file_notify,
    default         => $config_file_notify,
  }

  ### Manage configurations
  case $facts['os']['name'] {
    # On RedHat family we manage "/etc/sysconfig/network-scripts/ifcfg-${title}"
    'RedHat', 'CentOS', 'Scientific', 'OracleLinux', 'Fedora', 'AlmaLinux', 'Rocky': {
      # Configuration
      file { $config_file_path:
        ensure  => $ensure,
        content => $content,
        mode    => '0644',
        owner   => 'root',
        group   => 'root',
        notify  => $real_config_file_notify,
      }
    }

    # On Suse family we manage "/etc/sysconfig/network/ifcfg-${title}"
    'SLES', 'OpenSuSE': {
      # Prerequisites
      if $manage_prerequisites
      and 'VLAN_ID' in $extra_settings
      and !defined(Package['vlan']) {
        package { 'vlan':
          ensure => 'present',
        }
        Package['vlan'] -> File[$config_file_path]
      }
      if $manage_prerequisites
      and 'BRIDGE' in $settings
      and !defined(Package['bridge-utils']) {
        package { 'bridge-utils':
          ensure => 'present',
        }
        Package['bridge-utils'] -> File[$config_file_path]
      }
      # Configuration
      file { $config_file_path:
        ensure  => $ensure,
        content => $content,
        mode    => '0600',
        owner   => 'root',
        group   => 'root',
        notify  => $real_config_file_notify,
      }
    }

    # On Debian family we manage "/etc/network/interfaces.d/${title}.cfg"
    # or lines in /etc/sysconfig/network according to the value of
    # $psick::network::config_file_per_interface
    'Debian', 'Ubuntu', 'LinuxMint': {
      # Prerequisites
      if $manage_prerequisites
      and 'vlan-raw-device' in $settings
      and versioncmp('9.0', $facts['os']['release']['major']) >= 0
      and !defined(Package['vlan']) {
        package { 'vlan':
          ensure => 'present',
        }
      }
      # Configuration
      if $use_netplan {
        if $ipv4_address {
          if $ipv4_netmask {
            # TODO Handle ipv6 and multiple addresses
            $ipv4_cidr = netmask2cidr($ipv4_netmask)
            $addressv4 = ["${ipv4_address}/${ipv4_cidr}"]
          } else {
            fail('A ipv4_netmask must be set if ipv4_address is present')
          }
        } else {
          $addressv4 = undef
        }
        psick::network::netplan::interface { $interface:
          dhcp4     => $ipv4_dhcp,
          dhcp6     => $ipv6_dhcp,
          addresses => $addressv4,
          gateway4  => $ipv4_gateway,
          gateway6  => $ipv6_gateway,
        }
      } else {
        if $psick::network::config_file_per_interface {
          # Scenario with a file per interface
          if ! defined(File['/etc/network/interfaces.d']) {
            file { '/etc/network/interfaces.d':
              ensure => 'directory',
              mode   => '0755',
              owner  => 'root',
              group  => 'root',
            }
          }
          file { $config_file_path:
            ensure  => $ensure,
            content => $content,
            notify  => $real_config_file_notify,
          }
          if ! defined(File_line['config_file_per_interface']) {
            file_line { 'config_file_per_interface':
              ensure => $ensure,
              path   => '/etc/network/interfaces',
              line   => 'source /etc/network/interfaces.d/*.cfg',
              notify => $real_config_file_notify,
            }
          }
        } else {
          # Scenario with everything configured in /etc/network/interfaces
          if ! defined(Concat['/etc/network/interfaces']) {
            concat { '/etc/network/interfaces':
              mode   => '0644',
              owner  => 'root',
              group  => 'root',
              notify => $real_config_file_notify,
            }
          }
          concat::fragment { "interface-${title}":
            target  => '/etc/network/interfaces',
            content => $content,
            #         order   => pick($options['order'], 50),
          }

          if ! defined(Network::Interface['lo']) {
            psick::network::interface { 'lo':
              address => '127.0.0.1',
              method  => 'loopback',
              options => { 'order' => '05' },
            }
          }
        }
      }
    }

    # On Cumulus we manage "/etc/network/interfaces.d/${name}"
    # and line addon_scripts_support=1 in /etc/network/ifupdown2/ifupdown2.conf
    'CumulusLinux': {
      # Configuration
      file { $config_file_path:
        ensure  => $ensure,
        content => $content,
        notify  => $real_config_file_notify,
      }
      if ! defined(File_line['config_file_per_interface']) {
        file_line { 'config_file_per_interface':
          ensure => $ensure,
          path   => '/etc/network/ifupdown2/ifupdown2.conf',
          line   => 'addon_scripts_support=1',
          match  => 'addon_scripts_suppor*',
          notify => $real_config_file_notify,
        }
      }
    }

    # On Solaris we manage "/etc/hostname.${title}"
    # ipadm exec, host entry and network service
    'Solaris': {
      # Configuration
      if $facts['os']['release']['major'] == '5.11' {
        if ! defined(Service['svc:/network/physical:nwam']) {
          service { 'svc:/network/physical:nwam':
            ensure => stopped,
            enable => false,
          }
        }
        Service['svc:/network/physical:nwam']
        -> Service['svc:/network/physical:default']
        -> Exec["create ipaddr ${title}"]
        -> File[$config_file_path]
      }
      case $facts['os']['release']['major'] {
        '11','5': {
          if $ipv4_dhcp {
            $create_ip_command = "ipadm create-addr -T dhcp ${interface}/dhcp"
            $show_ip_command = "ipadm show-addr ${interface}/dhcp"
          } else {
            $create_ip_command = "ipadm create-addr -T static -a ${ipv4_address}/${ipv4_netmask} ${interface}/v4static"
            $show_ip_command = "ipadm show-addr ${interface}/v4static"
          }
        }
        default: {
          $create_ip_command = 'true '
          $show_ip_command = 'true '
        }
      }
      exec { "create ipaddr ${title}":
        command => $create_ip_command,
        unless  => $show_ip_command,
        path    => '/bin:/sbin:/usr/sbin:/usr/bin:/usr/gnu/bin',
      }
      file { $config_file_path:
        ensure  => $ensure,
        content => $content,
        require => Exec["create ipaddr ${title}"],
      }
      host { $facts['networking']['fqdn']:
        ensure       => present,
        ip           => $ipv4_address,
        host_aliases => [$facts['networking']['hostname']],
        require      => File[$config_file_path],
      }
      if ! defined(Service['svc:/network/physical:default']) {
        service { 'svc:/network/physical:default':
          ensure => running,
          enable => true,
        }
      }
      Service['svc:/network/physical:default'] ~> File[$config_file_path]
      Service['svc:/network/physical:default'] ~> Exec["create ipaddr ${interface}"]
    }

    # Other OS not supported
    default: {
      if ! $suppress_warnings {
        alert("${facts['os']['name']} not supported. Nothing done here. Set \$suppress_warnings to true to disable this message")
      }
    }
  }
}