Skip to content

Latest commit

 

History

History
 
 

lab-06.1-Validating-parameters

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Lab 6.1: Validating parameters

In the basics review lab, we asked you to create or update a simple module with a class that would accept a username. What would that class do if you passed in an integer value, or an array? What should it do?

In this lab, we'll be working with a class that also accepts a string parameter. Let's err on the side of good practice and add a datatype to ensure that a String value is passed in.

We'll start with a simple class like the following and make sure it generates a valid /etc/aliases file.

class system::aliases (
  $admin = 'root',
) {
# uses $admin to build the aliases file
file { '/etc/aliases':
  ensure  => file,
    owner   => 'root',
    group   => 'root',
    mode    => '0644',
    content => epp('system/aliases.epp', { admin => $admin }),
  }
  exec { '/usr/bin/newaliases':
    refreshonly => true,
    subscribe   => File['/etc/aliases'],
  }
}

Steps

Before running puppet agent -t, make sure you are still in your [modulepath]/system path and commit your code.

  1. Creating Aliases files.

    • Generate the aliases.epp template by copying from /etc/aliases
    • Edit templates/aliases.epp
    • Uncomment and update the last rule to use the value of $admin instead of the hardcoded name in there now.
  2. Add parameter validation.

    • Edit manifests/aliases.pp
    • Add the String datatype to the $admin parameter.
  3. Validate that the class works properly to start with

    pdk validate

  4. Deploy your codebase.

  5. Classify your node group with the system::aliases class.

    • Set the admin parameter to centos
  6. Run a puppet agent. puppet agent -t

Test the parameter validation

Ensure the class fails appropriately

  1. Edit examples/aliases_array.pp: declare with the admin parameter set to ["an", "array", "of", "names"]
  2. Edit examples/aliases_hash.pp: declare with the admin parameter set to {"first" => "jim", "second" => "bo"}
  3. Edit examples/aliases_number.pp: declare with the admin parameter set to 17
  4. puppet apply each example manifest

When typing data structures into the PE Node Classifier, the type of quote used is significant. For example, when typing in an array, quotes like ['an', 'array', 'of', 'strings'] would fail to parse correctly. Ensure that you use only double quotes when typing in the data structures shown.

Expected Output

Notice that in error output, Puppet will often display internal data type names, such as Tuple or Struct instead of the more human-friendly names like Array or Hash.

[root@training modules]# puppet apply examples/aliases.pp
Notice: Compiled catalog for master.puppetlabs.vm in environment production in 0.34 seconds
Notice: /Stage[main]/System/Aliases/File[/etc/aliases]/ensure: created
Notice: Applied catalog in 0.60 seconds
[root@training modules]# puppet apply examples/aliases_array.pp
Error: Evaluation Error: Error while evaluating a Resource Statement, Class[Review]: parameter 'admin' expects a String value, got Tuple at /etc/puppetlabs/code/modules/system/examples/aliases_array.pp:1:1 on node master.puppetlabs.vm
[root@training modules]# puppet apply examples/aliases_hash.pp
Error: Evaluation Error: Error while evaluating a Resource Statement, Class[Review]: parameter 'admin' expects a String value, got Struct at /etc/puppetlabs/code/modules/system/examples/aliases_hash.pp:1:1 on node master.puppetlabs.vm
[root@training modules]# puppet apply examples/aliases_number.pp
Error: Evaluation Error: Error while evaluating a Resource Statement, Class[Review]: parameter 'admin' expects a String value, got Integer at /etc/puppetlabs/code/modules/system/examples/aliases_number.pp:1:1 on node master.puppetlabs.vm

Extra credit

If you have extra time, you might try your hand at making a Pattern datatype that will allow either a username or an email address for the $admin parameter.

Solution

Your module structure should resemble

[root@training modules]# tree system/
system/
├── examples
│   ├── aliases.pp
│   ├── aliases_array.pp
│   ├── aliases_hash.pp
│   └── aliases_number.pp
├── manifests
│   └── aliases.pp
└── templates
    └── aliases.epp

Example file: system/templates/aliases.epp

...
marketing:    postmaster
sales:    postmaster
support:  postmaster

# trap decode to catch security attacks
decode:    root

# Person who should get root's mail
root:    <%= $admin %>

Example file: system/manifests/aliases.pp

class system::aliases (
    String $admin = 'root',
) {
    # uses $admin to build the aliases file
    file { '/etc/aliases':
      ensure  => file,
      owner   => 'root',
      group   => 'root',
      mode    => '0644',
      content => epp('system/aliases.epp', { admin => $admin }),
    }

    exec { '/usr/bin/newaliases':
      refreshonly => true,
      subscribe   => File['/etc/aliases'],
    }
}

Example file: system/examples/aliases.pp

user { 'admin':
  ensure => present,
}

class { 'system::aliases':
  admin   => 'admin',
  require => User['admin'],
}

Example file: system/examples/aliases_array.pp

class { 'system::aliases':
  admin   => ["an", "array", "of", "names"],
}

Example file: system/examples/aliases_hash.pp

class { 'system::aliases':
  admin   => {"first" => "jim", "second" => "bo"},
}

Example file: system/examples/aliases_number.pp

class { 'system::aliases':
  admin   => 17,
}

| Previous Lab | Next Lab |