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'],
}
}
Before running puppet agent -t
, make sure you are still in your [modulepath]/system
path and commit your code.
-
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.
- Generate the
-
Add parameter validation.
- Edit
manifests/aliases.pp
- Add the
String
datatype to the$admin
parameter.
- Edit
-
Validate that the class works properly to start with
pdk validate
-
Deploy your codebase.
-
Classify your node group with the
system::aliases
class.- Set the
admin
parameter tocentos
- Set the
-
Run a puppet agent.
puppet agent -t
Ensure the class fails appropriately
- Edit
examples/aliases_array.pp
: declare with theadmin
parameter set to["an", "array", "of", "names"]
- Edit
examples/aliases_hash.pp
: declare with theadmin
parameter set to{"first" => "jim", "second" => "bo"}
- Edit
examples/aliases_number.pp
: declare with theadmin
parameter set to17
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.
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
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.
[root@training modules]# tree system/
system/
├── examples
│ ├── aliases.pp
│ ├── aliases_array.pp
│ ├── aliases_hash.pp
│ └── aliases_number.pp
├── manifests
│ └── aliases.pp
└── 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 %>
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'],
}
}
user { 'admin':
ensure => present,
}
class { 'system::aliases':
admin => 'admin',
require => User['admin'],
}
class { 'system::aliases':
admin => ["an", "array", "of", "names"],
}
class { 'system::aliases':
admin => {"first" => "jim", "second" => "bo"},
}
class { 'system::aliases':
admin => 17,
}
| Previous Lab | Next Lab |