Facter and the params.pp File

Length: 00:21:03

Lesson Summary:

There is a bit of discord in the Puppet community as to whether we should use Hiera, the Puppet key-value store for all variables, or whether Hiera should be used alongside a params.pp file to store OS-related configurations. Regardless of where anyone stands on this, however, you're going to run into a params.pp file at some point in your Puppet use. So for this module, we'll be containing our OS-specific values in a params.pp, then use Hiera for our virtual hosts configuration.

We can create these OS-specific parameters because Puppet uses a cross-platform system profiling library called Facter. It records "facts" about a system, such as operating system family and kernel version, and reports it back to the Puppet Master so that the master has a high-level overview of all its nodes.

For example, if we want to view a full list of facts about our system, we can just run:

# facter

Or we could be more specific:

# facter os.family

params.pp

But how can we use these in our module? Well, right now our nginx module only works on Red Hat-based servers, like our CentOS 7 server. We want to play nice with Debian-based servers too, since we can run these against our Puppet Master. To do this, we're going to add a params.pp file. From the nginx module directory, run:

# pdk new class params

Open the file. We're going to break down our variables by our existing manifests:

install.pp:

  • package_name

config.pp:

  • config_path
  • config_source

service:

  • service_name

Note how the first word is always the same as the file name. That makes it easier for future users of our module to see where these are used!

Two of these, the package_name and service_name variables, are the same for both distros and will be provided as a default. The config parameters will be separated by operating system. Values such as whether or not we want the nginx package present or enabled will be added in Hiera, since those are generally determined by the node's purpose, not its operating system.

To add the default configurations, we just need to add these values as variables to our params class skeleton:

class nginx::params {
  $package_name = 'nginx'
  $service_name = 'nginx'
}

Then, for our distro-specific values, we can add a case statement, to ensure they are only used for the correct distro:

  case $::osfamily {
    'RedHat': {
      $config_path = '/etc/nginx/nginx.conf'
      $config_source = 'puppet:///modules/nginx/rh-nginx.conf'
    }
    'Debian': {
      $config_path = '/etc/nginx/nginx.conf'
      $config_source = 'puppet:///modules/nginx/deb-nginx.conf'
    }
  }

Now we need to reference these variables in the manifest files themselves:

init.pp

class nginx (
  $package_name  = $nginx::params::package_name,
  $config_path   = $nginx::params::config_path,
  $config_source = $nginx::params::config_source,
  $service_name  = $nginx::params::service_name,
) inherits nginx::params {

config.pp

class nginx::config (
  $config_path   = $nginx::params::config_path,
  $config_source = $nginx::params::config_source,
) inherits nginx::params {

install.pp

class nginx::install(
  $package_name  = $nginx::params::package_name,
) inherits nginx::params {

service.pp

class nginx::service (
  $service_name  = $nginx::params::service_name,
) inherits nginx::params {

Test the Changes

On the CentOS 7 Puppet agent, run:

# puppet agent -t

No changes should be made; however, we should receive no errors either.

We now want to test on a Debian-based machine. Lucky for us, the Puppet Master also manages itself, and it uses Ubuntu. Drop back down to the production folder, then open the manifests/site.pp file. Add an entry for your Puppet Master, including the nginx module:

node PUPPETMASTER.mylabserver.com {
  class { 'nginx': }
}

Save and exit.

On the master, force a Puppet run:

# puppet agent -t

To confirm that everything works, check nginx. Ensure the "managed by Puppet" header is in the /etc/nginx/nginx.conf file, and make sure the nginx daemon is running:

# which nginx
# head /etc/nginx/nginx.conf
# systemctl status nginx


This lesson is only available to Linux Academy members.

Sign Up To View This Lesson
Or Log In

Looking For Team Training?

Learn More