Write data: Create a test class

A test class writes the data it receives to a temporary file — on the agent when applying the catalog.

Hiera is used with Puppet code, so the first step is to create a Puppet class for testing.

  1. If you do not already use the roles and profiles method, create a module named profile. Profiles are wrapper classes that use multiple component modules to configure a layered technology stack. See The roles and profile method for more information.
  2. Use Puppet Development Kit ( PDK) to create a class called hiera_test.pp in your profile module.
  3. Add the following code you your hiera_test.pp file:

    # /etc/puppetlabs/code/environments/production/modules/profile/manifests/hiera_test.pp
    class profile::hiera_test (
      Boolean             $ssl,
      Boolean             $backups_enabled,
      Optional[String[1]] $site_alias = undef,
    ) {
      file { '/tmp/hiera_test.txt':
        ensure  => file,
        content => @("END"),
                   Data from profile::hiera_test
                   -----
                   profile::hiera_test::ssl: ${ssl}
                   profile::hiera_test::backups_enabled: ${backups_enabled}
                   profile::hiera_test::site_alias: ${site_alias}
                   |END
        owner   => root,
        mode    => '0644',
      }
    }
    The test class uses class parameters to request configuration data. Puppet looks up class parameters in Hiera, using <CLASS NAME>::<PARAMETER NAME> as the lookup key.

  4. Make a manifest that includes the class:

    # site.pp
    include profile::hiera_test

  5. Compile the catalog and observe that this fails because there are required values.
  6. To provide values for the missing class parameters, set these keys in your Hiera data. Depending on where in your hierarchy you want to set the parameters, you can add them to your common data, os data, or per-node data.

    Parameter Hiera key
    $ssl profile::hiera_test::ssl
    $backups_enabled profile::hiera_test::backups_enabled
    $site_alias profile::hiera_test::site_alias

  7. Compile again and observe that the parameters are now automatically looked up.

Related information