Skip to content

Latest commit

 

History

History
59 lines (39 loc) · 2.1 KB

File metadata and controls

59 lines (39 loc) · 2.1 KB
layout default
title Language: Data types: Booleans

Booleans are one-bit values, representing true or false.

The condition of an "if" statement expects an expression that resolves to a boolean value. All of Puppet's comparison operators resolve to boolean values, as do many functions.

Syntax

The boolean data type has two possible values: true and false. Literal booleans must be one of these two bare words (that is, not quoted).

Automatic conversion to boolean

If a non-boolean value is used where a boolean is required:

  • The undef value is converted to boolean false.
  • All other values are converted to boolean true.

Notably, this means the string values "" (zero-length string) and "false" both resolve to true.

If you want to convert other values to booleans with more permissive rules (0 as false, "false" as false, etc.), the puppetlabs-stdlib module includes str2bool and num2bool functions.

The Boolean data type

The data type of boolean values is Boolean.

On its own it matches only the values true or false.

Parameters

Boolean takes an optional parameter that narrows it to one of the two values:

notice(true =~ Boolean[true])   # true
notice(false =~ Boolean[true])  # false
notice(false =~ Boolean[false]) # true

This is useful when a parameter must be one specific value rather than either, such as a flag that may only ever be switched on:

class example (
  Boolean[true] $must_be_enabled = true,
) { }

Related data types

You can use abstract types to match values that might be boolean or might have some other value. For example, Optional[Boolean] will match true, false, or undef. Variant[Boolean, Enum["true", "false"]] will match stringified booleans as well as true booleans.