From ccb54b171fcedb01c79456c02c898d34670e0adb Mon Sep 17 00:00:00 2001 From: Anders Larsson Date: Wed, 29 Jul 2015 08:47:14 +0200 Subject: [PATCH] Allow HWADDR to be disabled --- README.md | 16 +++++ manifests/if/dynamic.pp | 4 ++ manifests/if/static.pp | 80 +++++++++++++------------ manifests/init.pp | 3 + spec/classes/network_global_spec.rb | 10 ++++ spec/defines/network_if_dynamic_spec.rb | 34 +++++++++++ spec/defines/network_if_static_spec.rb | 37 ++++++++++++ templates/ifcfg-eth.erb | 3 +- 8 files changed, 148 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 72c0b769..200b615e 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,15 @@ Normal interface - static (minimal): netmask => '255.255.255.128', } +Normal interface - static (minimal without HWADDR in ifcfg-file): + + network::if::static { 'eth0': + ensure => 'up', + ipaddress => '1.2.3.248', + netmask => '255.255.255.128', + manage_hwaddr => false, + } + Normal interface - static: network::if::static { 'eth1': @@ -75,6 +84,13 @@ Normal interface - dhcp (minimal): ensure => 'up', } +Normal interface - dhcp (minimal without HWADDR in ifcfg-file): + + network::if::dynamic { 'eth2': + ensure => 'up', + manage_hwaddr => false, + } + Normal interface - dhcp: network::if::dynamic { 'eth3': diff --git a/manifests/if/dynamic.pp b/manifests/if/dynamic.pp index 1f08d5dc..bfee572a 100644 --- a/manifests/if/dynamic.pp +++ b/manifests/if/dynamic.pp @@ -6,6 +6,7 @@ # # $ensure - required - up|down # $macaddress - optional - defaults to macaddress_$title +# $manage_hwaddr - optional - defaults to true # $bootproto - optional - defaults to "dhcp" # $userctl - optional - defaults to false # $mtu - optional @@ -43,6 +44,7 @@ define network::if::dynamic ( $ensure, $macaddress = undef, + $manage_hwaddr = true, $bootproto = 'dhcp', $userctl = false, $mtu = undef, @@ -66,6 +68,7 @@ # Validate booleans validate_bool($userctl) validate_bool($peerdns) + validate_bool($manage_hwaddr) network_if_base { $title: ensure => $ensure, @@ -73,6 +76,7 @@ netmask => '', gateway => '', macaddress => $macaddy, + manage_hwaddr => $manage_hwaddr, bootproto => $bootproto, userctl => $userctl, mtu => $mtu, diff --git a/manifests/if/static.pp b/manifests/if/static.pp index 09f1ca4a..836644d6 100644 --- a/manifests/if/static.pp +++ b/manifests/if/static.pp @@ -4,24 +4,25 @@ # # === Parameters: # -# $ensure - required - up|down -# $ipaddress - required -# $netmask - required -# $gateway - optional -# $ipv6address - optional -# $ipv6init - optional - defaults to false -# $ipv6gateway - optional -# $macaddress - optional - defaults to macaddress_$title -# $ipv6autoconf - optional - defaults to false -# $userctl - optional - defaults to false -# $mtu - optional -# $ethtool_opts - optional -# $peerdns - optional -# $ipv6peerdns - optional - defaults to false -# $dns1 - optional -# $dns2 - optional -# $domain - optional -# $scope - optional +# $ensure - required - up|down +# $ipaddress - required +# $netmask - required +# $gateway - optional +# $ipv6address - optional +# $ipv6init - optional - defaults to false +# $ipv6gateway - optional +# $manage_hwaddr - optional - defaults to true +# $macaddress - optional - defaults to macaddress_$title +# $ipv6autoconf - optional - defaults to false +# $userctl - optional - defaults to false +# $mtu - optional +# $ethtool_opts - optional +# $peerdns - optional +# $ipv6peerdns - optional - defaults to false +# $dns1 - optional +# $dns2 - optional +# $domain - optional +# $scope - optional # # === Actions: # @@ -57,6 +58,7 @@ $ipv6init = false, $ipv6gateway = undef, $macaddress = undef, + $manage_hwaddr = true, $ipv6autoconf = false, $userctl = false, $mtu = undef, @@ -88,27 +90,29 @@ validate_bool($ipv6autoconf) validate_bool($peerdns) validate_bool($ipv6peerdns) + validate_bool($manage_hwaddr) network_if_base { $title: - ensure => $ensure, - ipv6init => $ipv6init, - ipaddress => $ipaddress, - ipv6address => $ipv6address, - netmask => $netmask, - gateway => $gateway, - ipv6gateway => $ipv6gateway, - ipv6autoconf => $ipv6autoconf, - macaddress => $macaddy, - bootproto => 'none', - userctl => $userctl, - mtu => $mtu, - ethtool_opts => $ethtool_opts, - peerdns => $peerdns, - ipv6peerdns => $ipv6peerdns, - dns1 => $dns1, - dns2 => $dns2, - domain => $domain, - linkdelay => $linkdelay, - scope => $scope, + ensure => $ensure, + ipv6init => $ipv6init, + ipaddress => $ipaddress, + ipv6address => $ipv6address, + netmask => $netmask, + gateway => $gateway, + ipv6gateway => $ipv6gateway, + ipv6autoconf => $ipv6autoconf, + macaddress => $macaddy, + manage_hwaddr => $manage_hwaddr, + bootproto => 'none', + userctl => $userctl, + mtu => $mtu, + ethtool_opts => $ethtool_opts, + peerdns => $peerdns, + ipv6peerdns => $ipv6peerdns, + dns1 => $dns1, + dns2 => $dns2, + domain => $domain, + linkdelay => $linkdelay, + scope => $scope, } } # define network::if::static diff --git a/manifests/init.pp b/manifests/init.pp index ff3cbbcf..82f5469f 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -53,6 +53,7 @@ # $ipaddress - required # $netmask - required # $macaddress - required +# $manage_hwaddr - optional - defaults to true # $gateway - optional # $bootproto - optional # $userctl - optional - defaults to false @@ -99,6 +100,7 @@ $ipaddress, $netmask, $macaddress, + $manage_hwaddr = true, $gateway = undef, $ipv6address = undef, $ipv6gateway = undef, @@ -130,6 +132,7 @@ validate_bool($ipv6autoconf) validate_bool($ipv6peerdns) validate_bool($check_link_down) + validate_bool($manage_hwaddr) # Validate our regular expressions $states = [ '^up$', '^down$' ] validate_re($ensure, $states, '$ensure must be either "up" or "down".') diff --git a/spec/classes/network_global_spec.rb b/spec/classes/network_global_spec.rb index fc33a843..074b76e7 100644 --- a/spec/classes/network_global_spec.rb +++ b/spec/classes/network_global_spec.rb @@ -113,6 +113,16 @@ end end + context 'manage_hwaddr = foo' do + let(:params) {{ :manage_hwaddr => 'foo' }} + + it 'should fail' do + expect { + should raise_error(Puppet::Error, /$manage_hwaddr is not a boolean. It looks to be a String./) + } + end + end + end context 'on a supported operatingsystem, custom parameters' do diff --git a/spec/defines/network_if_dynamic_spec.rb b/spec/defines/network_if_dynamic_spec.rb index 1de7eb37..82903698 100644 --- a/spec/defines/network_if_dynamic_spec.rb +++ b/spec/defines/network_if_dynamic_spec.rb @@ -125,4 +125,38 @@ it { should contain_service('network') } end + context 'optional parameters - manage_hwaddr' do + let(:title) { 'eth0' } + let :params do { + :ensure => 'up', + :manage_hwaddr => false, + } + end + let :facts do { + :osfamily => 'RedHat', + :macaddress_eth0 => 'bb:cc:bb:cc:bb:cc', + } + end + it { should contain_file('ifcfg-eth0').with( + :ensure => 'present', + :mode => '0644', + :owner => 'root', + :group => 'root', + :path => '/etc/sysconfig/network-scripts/ifcfg-eth0', + :notify => 'Service[network]' + )} + it 'should contain File[ifcfg-eth0] with required contents' do + verify_contents(catalogue, 'ifcfg-eth0', [ + 'DEVICE=eth0', + 'BOOTPROTO=none', + 'ONBOOT=yes', + 'HOTPLUG=yes', + 'TYPE=Ethernet', + 'NM_CONTROLLED=no', + ]) + end + it { should contain_service('network') } + end + + end diff --git a/spec/defines/network_if_static_spec.rb b/spec/defines/network_if_static_spec.rb index cf2f91b2..10250e22 100644 --- a/spec/defines/network_if_static_spec.rb +++ b/spec/defines/network_if_static_spec.rb @@ -175,4 +175,41 @@ it { should contain_service('network') } end + context 'optional parameters - manage_hwaddr' do + let(:title) { 'eth0' } + let :params do { + :ensure => 'up', + :ipaddress => '1.2.3.4', + :netmask => '255.255.255.0', + :manage_hwaddr => false, + } + end + let :facts do { + :osfamily => 'RedHat', + :macaddress_eth0 => 'bb:cc:bb:cc:bb:cc', + } + end + it { should contain_file('ifcfg-eth0').with( + :ensure => 'present', + :mode => '0644', + :owner => 'root', + :group => 'root', + :path => '/etc/sysconfig/network-scripts/ifcfg-eth0', + :notify => 'Service[network]' + )} + it 'should contain File[ifcfg-eth0] with required contents' do + verify_contents(catalogue, 'ifcfg-eth0', [ + 'DEVICE=eth0', + 'BOOTPROTO=none', + 'ONBOOT=yes', + 'HOTPLUG=yes', + 'TYPE=Ethernet', + 'IPADDR=1.2.3.4', + 'NETMASK=255.255.255.0', + 'NM_CONTROLLED=no', + ]) + end + it { should contain_service('network') } + end + end diff --git a/templates/ifcfg-eth.erb b/templates/ifcfg-eth.erb index 3f17d2d1..3606f2ea 100644 --- a/templates/ifcfg-eth.erb +++ b/templates/ifcfg-eth.erb @@ -3,7 +3,8 @@ ### DEVICE=<%= @interface %> BOOTPROTO=<%= @bootproto %> -<% if @macaddress and @macaddress != '' %>HWADDR=<%= @macaddress %> +<% if @manage_hwaddr and @macaddress and @macaddress != '' -%> +HWADDR=<%= @macaddress %> <% end -%> ONBOOT=<%= @onboot %> HOTPLUG=<%= @onboot %>