Skip to content
Snippets Groups Projects
kernel-module.pp 2.09 KiB
Newer Older
Adam Lewenberg's avatar
Adam Lewenberg committed
#
# Definition for loading kernel modules.
# This is taken directly from:
#  http://reductivelabs.com/trac/puppet/wiki/Recipes/KernelModules

define base::os::kernel-module ($ensure) {
    $modulesfile = $operatingsystem ? { 
        'debian' => '/etc/modules',
        'ubuntu' => '/etc/modules',
        'redhat' => '/etc/rc.modules',
    }

    case $operatingsystem {
        'redhat': { 
            file { '/etc/rc.modules': 
                ensure => file, 
                mode => 755 
            } 
        }
    }
    case $ensure {
        present: {
            exec { "insert_module_${name}":
                command => $operatingsystem ? {
                    'debian' => "/bin/echo '${name}' >> '${modulesfile}'",
                    'ubuntu' => "/bin/echo '${name}' >> '${modulesfile}'",
                    'redhat' => "/bin/echo '/sbin/modprobe ${name}' >> '${modulesfile}'",
                },
                unless => "/bin/grep -q '${name}' '${modulesfile}'"
            }
            exec { "/sbin/modprobe ${name}": 
                unless => "/bin/grep -q '^${name} ' '/proc/modules'" 
            }
        }
        absent: {
            exec { "/sbin/modprobe -r ${name}": 
                onlyif => "/bin/grep -q '^${name} ' '/proc/modules'" 
            }
            exec { "remove_module_${name}":
                command => $operatingsystem ? {
                    'debian' => "/usr/bin/perl -ni -e 'print unless /^\\Q${name}\\E\$/' '${modulesfile}'",
                    'ubuntu' => "/usr/bin/perl -ni -e 'print unless /^\\Q${name}\\E\$/' '${modulesfile}'",
                    'redhat' => "/usr/bin/perl -ni -e 'print unless /^\\Q/sbin/modprobe ${name}\\E\$/' '${modulesfile}'",
                },
                onlyif => $operatingsystem ? {
                    'debian' => "/bin/grep -qFx '${name}' '${modulesfile}'",
                    'ubuntu' => "/bin/grep -qFx '${name}' '${modulesfile}'",
                    'redhat' => "/bin/grep -q '^/sbin/modprobe ${name}' '${modulesfile}'",
                },
            }
        }
        default: { err ( "unknown ensure value ${ensure}" ) }
    }
}