Defined Type: libvirt::network

Defined in:
manifests/network.pp

Overview

libvirt::network

Parameters:

  • ensure (String) (defaults to: 'present')
  • bridge (String) (defaults to: '')

    Name of the bridge device to use for this network.

  • forward_mode (String) (defaults to: 'bridge')

    Network forward mode. Valid modes are 'nat', 'route', 'bridge', 'private', 'vepa', 'passthrough' and 'hostdev'. The default is 'bridge'.

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

    Set this to 'openvswitch' for an Open vSwitch bridge. Leave undefined otherwise.

  • portgroups (Array) (defaults to: [])

    Array of hashes defining portgroups. This only works for Open vSwitch networks. The hash supports the following keys:

    • name: Name of the portgroup.
    • trunk: Set to true if this is a trunk port. In this case, the vlan_tag element must contain an array of allowed VLAN tags.
    • vlan_tag: VLAN tag for this portgroup.
  • autostart (Boolean) (defaults to: true)

    Wheter to start this network on boot or not. Defaults to true.

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

    The interface to forward, useful in bridge and route mode

  • forward_interfaces (Array) (defaults to: [])

    An array of interfaces to forwad

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

    The ip address for the device

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

    The netmask for the ip address

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

    Optional dhcp range start

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

    Optional dhcp range end



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
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
# File 'manifests/network.pp', line 42

define libvirt::network (
  String           $ensure             = 'present',
  String           $bridge             = '',
  String           $forward_mode       = 'bridge',
  Optional[String] $forward_dev        = undef,
  Array            $forward_interfaces = [],
  Optional[String] $virtualport_type   = undef,
  Array            $portgroups         = [],
  Boolean          $autostart          = true,
  Optional[String] $ip_address         = undef,
  Optional[String] $ip_netmask         = undef,
  Optional[String] $dhcp_start         = undef,
  Optional[String] $dhcp_end           = undef,
) {

  include ::libvirt

  if ($ensure != 'absent') {
    exec {"libvirt-network-${name}":
      command  => join(['f=$(mktemp) && echo "',
                        template('libvirt/network.xml.erb'),
                        '" > $f && virsh net-define $f && rm $f']),
      provider => 'shell',
      creates  => "${libvirt::config_dir}/qemu/networks/${name}.xml",
      require  => Anchor['libvirt::installed'],
    }

    if $libvirt::diff_dir != '' {
      file {"${libvirt::diff_dir}/networks/${name}.xml":
        content => template('libvirt/network.xml.erb'),
      }
    }

    if ($autostart) {
      exec {"libvirt-network-autostart-${name}":
        command  => "virsh net-autostart ${name}",
        provider => 'shell',
        creates  => "${libvirt::config_dir}/qemu/networks/autostart/${name}.xml",
        require  => Exec["libvirt-network-${name}"],
      }

      exec {"libvirt-network-start-${name}":
        command  => "virsh net-start ${name}",
        provider => 'shell',
        unless   => "virsh net-list | tail -n +3 | cut -d ' ' -f 2 | \
                    grep -q ^${name}$",
        require  => Exec["libvirt-network-${name}"],
      }
    }
  } else {
    exec {"libvirt-delete-network-${name}":
      command  => "virsh net-destroy ${name}",
      provider => 'shell',
      onlyif   => "virsh net-list | tail -n +3 | cut -d ' ' -f 2 | \
                    grep -q ^${name}$",
      require  => Anchor['libvirt::installed'],
    }
    exec {"libvirt-network-disable-autostart-${name}":
      command  => "virsh net-autostart ${name} --disable",
      provider => 'shell',
      onlyif   => "test -L /etc/libvirt/qemu/networks/autostart/${name}.xml",
      require  => Exec["libvirt-delete-network-${name}"],
    }
    exec {"libvirt-undefine-network-${name}":
      command  => "virsh net-undefine ${name}",
      provider => 'shell',
      onlyif   => "test -f /etc/libvirt/qemu/networks/${name}.xml",
      require  => Exec["libvirt-network-disable-autostart-${name}"],
    }
  }
}