Each value in the Puppet language has a data type, like “string.” There is also a set of values whose data type is “data type.”
These values represent the other data types. For example, the value String
represents the data type of strings.
(The value that represents the data type of these values is Type
.)
You can use these special values to examine a piece of data or enforce rules — for example, you can test whether something is a string with the expression $possible_string =~ String
, or specify that a class parameter requires string values like class myclass (String $string_parameter = "default value") { ... }
.
Data types are written as unquoted upper-case words, like String
.
Data types sometimes take parameters, which make them more specific. (For example, String[8]
is the data type of strings with a minimum of eight characters.)
Each known data type defines how many parameters it accepts, what values those parameters take, and the order in which they must be given. Some of the abstract types require parameters, and most types have some optional parameters available.
The general form of a data type is:
[
) after the type’s name. (There can’t be any space between the name and the bracket.)]
).For example:
Variant[Boolean, Enum['true', 'false', 'running', 'stopped']]
This is an abstract data type (Variant
) which takes any number of data types as parameters; one of the parameters we provided is another abstract data type (Enum
) that takes any number of strings as parameters.
Note on Required Parameters
The only situation where you can leave out required parameters is if you’re referring to the type itself; that is,
Type[Variant]
is legal, even thoughVariant
has required parameters.
Data types are useful in parameter lists, match (=~
) expressions, case statements, and selector expressions. There are also a few less common uses for them.
Classes, defined types, and lambdas all let you specify parameters, which let your code request data from a user or some other source.
Generally, your code expects each parameter to be a specific kind of data. You can enforce that expectation by putting a data type before that parameter’s name in the parameter list. At evaluation time, Puppet will raise an error if any parameter receives an illegal value.
For example:
class ntp (
Boolean $service_manage = true,
Boolean $autoupdate = false,
String $package_ensure = 'present',
# ...
) {
# ...
}
If you tried to set $autoupdate
to a string like "true"
, Puppet would raise an error, since it expects a legit boolean value.
Abstract data types can let you write more sophisticated and flexible restrictions. For example, this $puppetdb_service_status
parameter would accept values of true
, false
, "true"
, "false"
, "running"
, and "stopped"
, and raise an error for any other value:
class puppetdb::server (
Variant[Boolean, Enum['true', 'false', 'running', 'stopped']]
$puppetdb_service_status = $puppetdb::params::puppetdb_service_status,
) inherits puppetdb::params {
# ...
}
Case statements and selector expressions both allow data types as their cases. Puppet will choose a data type case if the control expression resolves to a value of that data type. For example:
$enable_real = $enable ? {
Boolean => $enable,
String => str2bool($enable),
Numeric => num2bool($enable),
default => fail('Illegal value for $enable parameter'),
}
The =~
and !~
match operators can accept a data type on the right operand, and will test whether the left operand is a value of that data type.
For example: 5 =~ Integer
and 5 =~ Integer[1,10]
both resolve to true
.
The built-in assert_type
function takes a value and a data type, and will raise errors if your code encounters an illegal value. It’s basically a shorthand for an if
statement with a non-match (!~
) expression and a fail()
function call.
You can also provide data types as both operands for the ==
, !=
, <
, >
, <=
, and >=
comparison operators, which tests whether two data types are equal, whether one is a subset of another, etc. This feature doesn’t have any particular practical use.
The puppetlabs/stdlib
module includes a type_of
function, which can return the type of any value. E.g. type_of(3)
returns Integer[3,3]
.
The following data types are available in Puppet:
These are the “real” data types, which make up the most common values you’ll interact with in the Puppet language.
Resource references and class references are implemented as data types, although they behave somewhat differently from other values.
Abstract data types let you do more sophisticated or permissive type checking.
Type
Data TypeThe data type of literal data type values is Type
.
By default, Type
matches any value that represents a data type, such as Integer
, Integer[0,800]
, String
, Enum["running", "stopped"]
, etc.
You can use parameters to restrict which values Type
will match.
The full signature for Type
is:
Type[<ANY DATA TYPE>]
All of these parameters are optional. They must be listed in order; if you need to specify a later parameter, you must specify values for any prior ones.
Position | Parameter | Data Type | Default Value | Description |
---|---|---|---|---|
1 | Any data type | Type |
Any |
A data type, which will cause the resulting Type object to only match against that type or types that are more specific subtypes of that type. |
Type
— matches any data type, such as Integer
, String
, Any
, or Type
.Type[String]
— matches the data type String
, as well as any of its more specific subtypes like String[3]
or Enum["running", "stopped"]
.Type[Resource]
— matches any Resource
data type — that is, any resource reference.