Open source Puppet 6.10

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.

The general form of a data type is:
  • 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 ].

The following example uses an abstract data type 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']]
Note: When parameters are required, you must specify them. The only situation when you can leave out required parameters is if you’re referring to the type itself. For example, 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.

For example, consider the following class. If you tried to set $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',
  # ...
) {
  # ...
}
Abstract data types let you write more sophisticated and flexible restrictions. For example, this $puppetdb_service_statusparameter accepts values of truefalse"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

Case statements and selector expressions allow data types as their cases. Puppet chooses 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'),
}

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

These are the "real" data types, which make up the most common values you'll interact with in the Puppet language.
String Integer Float Numeric
Boolean Array Hash Regexp
Sensitive Undef Default

Resource and class references

Resource references and class references are implemented as data types, although they behave somewhat differently from other values.
Resource and Class

Abstract data types

Abstract data types let you do more sophisticated or permissive type checking.
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 IntegerInteger[0,800]String, or Enum["running", "stopped"]. You can use parameters to restrict which values Type matches.

Parameters

The full signature for Type is:
Type[<ANY DATA TYPE>]
This parameter is optional.
PositionParameterData typeDefaultDescription
1Any data typeTypeAnyA data type, which causes the resulting Type object to only match against that type or types that are more specific subtypes of that type.
Examples:
Type
Matches any data type, such as IntegerStringAny, 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.
Back to top
The page rank or the 1 our of 5 rating a user has given the page.
The email address of the user submitting feedback.
The URL of the page being ranked/rated.