Description
When a parameter has a default value (e.g. String $param = 'default'
), Puppet’s documentation and language spec both say that passing undef
should act as if the parameter was omitted—triggering the default. However:
- With exported resources (
@@
), if you explicitly set a parameter toundef
, Puppet’s type validation fails at compile time. Instead of using the default value, Puppet treats that undef as a type mismatch (if the parameter is typed). - With virtual resources (
@
), the same issue only manifests if the resource is never realized—in that case,undef
also triggers a type mismatch instead of using the default.
Virtual Resource Example
# @summary virtual.pp: A really simple defined type with 1 typed parameter with a default value
#
# @param param1 A simple `String` parameter with a valid default value
define my_module::example (
String $param1 = 'test',
) {
notice($param1)
}
@my_module::example { 'param set with undef (virtual resource)':
param1 => undef,
}
❯ puppet apply -t virtual.pp
Info: Loading facts
Error: My_module::Example[param set with undef (virtual resource)]: parameter 'param1' expects a String value, got Undef on node example
If I realize
the resource (realize(My_module::Example['param set with undef (virtual resource)'])
), it works fine.
Notice: Scope(My_module::Example[param set with undef (virtual resource)]): test
Exported Resources Example
# @summary exported.pp: A really simple defined type with 1 typed parameter with a default value
#
# @param param1 A simple `String` parameter with a valid default value
define my_module::example (
String $param1 = 'test',
) {
notice($param1)
}
@@my_module::example { 'param set with undef (exported resource)':
param1 => undef,
}
(When running with puppet apply
puppetdb might not be configured, but the bug still triggers in the same way as if doing an agent run)
❯ puppet apply -t exported.pp
Info: Loading facts
Warning: You cannot collect exported resources without storeconfigs being set; the export is ignored (file: /home/halexfis/exported.pp, line: 10, column: 1)
Error: My_module::Example[param set with undef (exported resource)]: parameter 'param1' expects a String value, got Undef on node example
This only appears to be an issue with incorrect parameter validation. In a scenario where I use puppet agent
, (with the server connected to PuppetDB), if I export a resource that doesn't have any type validation, (so doesn't trigger the bug), parameters I 'set' to undef
do not appear in PuppetDB, so when later collected, default values are correctly used.
This looks like a bug that has existed in Puppet for a long time (since Puppet 4.3.0 AFAICT).