Defined Type: interface::route

Defined in:
modules/interface/manifests/route.pp

Summary

Adds a static route for a defined prefix

Overview

SPDX-License-Identifier: Apache-2.0

Parameters:

  • address (Interface::RouteTarget)

    Destination address without the prefix

  • nexthop (Stdlib::IP::Address::Nosubnet)

    Next hop used to reach the destination address

  • ipversion

    IPv4 or IPv6 route

  • interface (String[1]) (defaults to: $facts['networking']['primary'])

    Exit interface

  • mtu (Optional[Integer[512,1500]]) (defaults to: undef)

    MTU (lock) to use for that destination

  • prefixlen (Optional[Integer[0,128]]) (defaults to: undef)

    Destination's prefix

  • table (Optional[String[1]]) (defaults to: undef)

    Routing table to use for this route

  • persist (Boolean) (defaults to: false)

    Create a post-up entry in /etc/network/interfaces to persist the route



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'modules/interface/manifests/route.pp', line 11

define interface::route(
    Interface::RouteTarget        $address,
    Stdlib::IP::Address::Nosubnet $nexthop,
    String[1]                     $interface = $facts['networking']['primary'],
    Boolean                       $persist   = false,
    Optional[Integer[512,1500]]   $mtu       = undef,
    Optional[Integer[0,128]]      $prefixlen = undef,
    Optional[String[1]]           $table     = undef,
) {
    $nexthop_version = wmflib::ip_family($nexthop)
    $ipversion = $address ? {
        'default' => $nexthop_version,
        default   => wmflib::ip_family($address),
    }

    if $ipversion != $nexthop_version {
        fail("\$address (${address}) and \$nexthop (${nexthop}) need to use the same ip family")
    }

    if $address == 'default' {
        $prefix = 'default'
    } else {
        $_prefixlen = $prefixlen.lest || {
            if $ipversion == 4 { 32 } else { 128 }
        }
        $prefix = "${address}/${_prefixlen}"
    }

    $mtu_cmd = $mtu.then |$x| { "mtu lock ${x}" }
    $int_cmd = $interface.then |$x| { "dev ${x}" }
    $table_cmd = $table.then |$x| { " table ${x}" }
    $table_require = $table.then |$x| { Interface::Routing_table[$x] }
    $v6switch = ($ipversion == 6).bool2str('-6', '')
    $route_cmd = "ip ${v6switch} route"

    # We split and join to get rid of excessive whitespace
    $add_command = "${route_cmd} add ${prefix} via ${nexthop} ${mtu_cmd} ${int_cmd}${table_cmd}"
                    .split(/\s+/)
                    .join(' ')
    # Insert the route, same command for v6 and v4
    # But show command needs '-6' to display v6 routes
    # When a /32 or /128 prefix lenght is present, 'ip route show' strips it
    $show_command = "${route_cmd} show ${prefix}${table_cmd} | grep -q via"
    exec { $add_command:
        path    => '/bin:/usr/bin',
        unless  => $show_command,
        require => $table_require,
    }

    # if the interface is managed by Puppet, ensure it's created first
    Exec <| tag == "interface-create-${interface}" |>
        -> Exec[$add_command]

    # persisting the route is optional, but if you don't do it, it won't survive
    # a reboot of the server and the route will be missing until the next puppet run.
    if $persist {
        interface::post_up_command { "${title}_persist":
            interface => $interface,
            command   => $add_command,
        }
    }
}