Open source Puppet 6.11



Hashes are written as a pair of curly braces {} containing any number of key-value pairs. A key is separated from its value by an arrow (sometimes called a fat comma or hash rocket) =>, and adjacent pairs are separated by commas. An optional trailing comma is allowed between the final value and the closing curly brace.
# Equivalent:
Hash keys can be any data type, but generally, you should use only strings. Put quotation marks around keys that are strings. Don't assign a hash with non-string keys to a resource attribute or class parameter, because Puppet cannot serialize non-string hash keys into the catalog.
  'key2 => {  'key3' =>  'val3',  }, 
  'key4' => true,
  'key5' => 12345,

Accessing values

Access hash members with their key inside square brackets:
$myhash = { key       => "some value",
            other_key => "some other value" }
notice( $myhash[key] )
This example manifest would log some value as a notice.
If you try to access a nonexistent key from a hash, its value is undef:
$cool_value = $myhash[absent_key] # Value is undef
Nested arrays and hashes can be accessed by chaining indexes:
                                https => 443 },
               vhost_name  => '',
               server_name => { mirror0 => '',
                                mirror1 => '' }
notice ( $main_site[port][https] )
This example manifest would log 443 as a notice.

Merging hashes


Where a key exists in both hashes, the merged hash uses the value of the key in the hash to the right of the + operator. For example:
$result = $values + $overrides

Additional functions for hashes

The  puppetlabs-stdlib  module contains useful functions for working with hashes, including:
has_key keys validate_hash
is_hash merge values

The Hash data type

The data type of hashes is Hash. By default, Hash matches hashes of any size, as long as their keys match the abstract type Scalar and their values match the abstract type Data. You can use parameters to restrict which values Hash  matches.


The full signature for Hash is:
These parameters are optional. You must specify both key type and value type if you’re going to specify one of them. The parameters must be listed in order; if you need to specify a later parameter, you must also specify values for any prior ones.
Position Parameter Data type Default value Description
1 Key type Type Scalar What kinds of values can be used as keys. If you specify a key type, a value type is mandatory.
3 Minimum size Integer 0 The minimum number of key-value pairs in the hash. This parameter accepts the special value default, which uses its default value.
4 Maximum size Integer infinite The maximum number of key-value pairs in the hash. This parameter accepts the special value default, which uses its default value.
Matches a hash of any length; all keys must match Scalar and any values must match Data.
Hash[Integer, String]
Matches a hash that uses integers for keys and strings for values.
Hash[Integer, String, 1]
Same as previous, and requires a non-empty hash.
Hash[Integer, String, 1, 8]
Same as previous, and with a maximum size of eight key-value pairs.


Other abstract types, particularly Variant and Enum, are useful when specifying a value type for hashes that include multiple kinds of data.

Back to top
The email address of the user submitting feedback.
The URL of the page being ranked/rated.