puppet-lint-optional_default-check
A puppet-lint plugin to check that Optional parameters don't default to something other than undef.
Installing
From the command line
$ gem install puppet-lint-optional_default-check
In a Gemfile
gem 'puppet-lint-optional_default-check', :require => false
Checks
Optional parameter defaults to something other than undef
An Optional parameter in Puppet is one where undef is an allowed value.
It is normally a mistake to set the default of an Optional parameter to something other than undef.
This is because it's not possible to 'pass' undef as the value to use for a parameter when declaring a class or defined type.
When you try to set a parameter to undef, Puppet actually uses the class's default value for that parameter, not undef itself.
(The caveat is that it is possible to use hiera to override a non undef default back to undef, but in practice, doing this is quite rare.)
A defined type with an mandatory (no default), Optional parameter will raise a warning.
The plugin will not raise a warning if a class Optional parameter doesn't have a default.
Mandatory parameters can have defaults set in hiera, and several modules do use ~ for this.
What you have done
class foo (
Optional[Integer] $port = 8080,
){
}
What you should have done
class foo (
Integer $port = 8080,
){
}
or
class foo (
Optional[Integer] $port = undef,
){
}
Copyright
Copyright 2021 Alexander Fisher
Licensed under the MIT License.