Thursday, May 29, 2008

Use Puppet to Set the Ethernet Speed

The way our network is configured, the workstations perform much better if their nic speed is set, rather than using auto negotiation.

It's easy enough to go out to each of the machines to do this, but it is much easier to let Puppet do the grunt work.

This is a class that will do just that for Red Hat based workstations (the class resides in its own file /etc/puppet/manifests/classes/nic_speed.pp):
class nic_speed {
   case $hostname {
"host01",
"host02",
"host07",
"host08",
"host10": {
# Set the nics to 100 half
append_if_no_such_line{ eth0:
file => "/etc/sysconfig/network-scripts/ifcfg-eth0",
line => 'ETHTOOL_OPTS="speed 100 duplex half autoneg off"'}
}
default: { }
}
}

The hosts listed in the case statement will have the ETHTOOL_OPTS line appended to their ifcfg-eth0 file, all others will use the default, which is to do nothing.

append_if_no_such_line is not native functionality of Puppet and is part of a custom class called cfengine.pp that adds a few commonly used features of CFengine:
# /etc/puppet/manifests/classes/cfengine.pp

define append_if_no_such_line($file, $line, $refreshonly = 'false') {
exec { "/bin/echo '$line' >> '$file'":
unless => "/bin/grep -Fxqe '$line' '$file'",
path => "/bin",
refreshonly => $refreshonly,
}
}

define delete_lines($file, $pattern) {
exec { "sed -i -r -e '/$pattern/d' $file":
path => "/bin",
onlyif => "/bin/grep -E '$pattern' '$file'",
}
}

No comments: