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 with class myclass (String $string_parameter = "default value") { ... }
.
Syntax
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.
An upper-case word matching one of the known data types.
- Sometimes, a set of parameters, which consists of:
An opening square bracket
[
after the type’s name. There can’t be any space between the name and the bracket.A comma-separated list of values or expressions. Arbitrary whitespace is allowed, but you can’t have a trailing comma after the final value.
A closing square bracket
]
.
Variant
, which takes any number of data types as parameters. One of the parameters provided in the example is another abstract data type Enum
, which takes any number of strings as parameters:Variant[Boolean, Enum['true', 'false', 'running', 'stopped']]
Type[Variant]
is legal, even though Variant
has required parameters.Usage
Data types are useful in parameter lists, match (=~
) expressions, case statements, and selector expressions. There are also a few less common uses for them.
Specify data types in your Puppet code whenever you can, aligning them in columns. Type your class parameters wherever possible, and be specific when using a type. For example, use an Enum
for input validation, instead of using a String
and checking the contents of the string in the code. You have the option to specify String[1]
instead of String
, because you might want to enforce non-empty strings. Specify data types as deeply as possible, without affecting readability. If readability becomes a problem, consider creating a custom data type alias.
Parameter lists
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 raises an error if a parameter receives an illegal value.
$autoupdate
to a string like "true"
, Puppet would raise an error, because it expects a Boolean value:class ntp (
Boolean $service_manage = true,
Boolean $autoupdate = false,
String $package_ensure = 'present',
# ...
) {
# ...
}
$puppetdb_service_statusparameter
accepts values of true
, false
, "true"
, "false"
, "running"
, and "stopped"
, and raises 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 {
# ...
}
Cases
$enable_real = $enable ? {
Boolean => $enable,
String => str2bool($enable),
Numeric => num2bool($enable),
default => fail('Illegal value for $enable parameter'),
}
Match expressions
The match operators =~
and !~
accept a data type on the right operand, and 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
.
Less common uses
The built-in function assert_type
takes a value and a data type, and raises errors if your code encounters an illegal value. Think of it as 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 comparison operators ==
, !=
, <
, >
, <=
, and >=
, to test whether two data types are equal, whether one is a subset of another, and so on.
Obtaining data types
The built-in function type
returns the type of any value. For example, type(3)
returns Integer[3,3]
.
List of Puppet data types
The following data types are available in the Puppet language.
Core data types
Resource and class references
Resource and Class
|
Abstract data types
Scalar
|
Collection
|
Variant
|
Data
|
Pattern
|
Enum
|
Tuple
|
Struct
|
Optional
|
Catalogentry
|
Type
|
Any
|
Callable
|
The Type
data type
The 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
, or Enum["running", "stopped"]
. You can use parameters to restrict which values Type
matches.
Parameters
Type
is:Type[<ANY DATA TYPE>]
This parameter is optional.
Position | Parameter | Data type | Default | Description |
---|---|---|---|---|
1 | Any data type | Type | Any | A data type, which causes 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
, orType
. -
Type[String]
- Matches the data type
String
, as well as any of its more specific subtypes likeString[3]
orEnum["running", "stopped"]
. Type[Resource]
- Matches any
Resource
data type — that is, any resource reference.