Hiera

Hiera and Yaml belong together (as do Puppet and Hiera). There are some limitations of using Yaml in Hiera.

Data

Source

# key/value
postfix::interface:          # value is nil
postfix::interface: ''       # value is empty
postfix::interface: 'all'    # value is "all"

# arrays
postfix::relays:             # value is nil
  -
postfix::relays: []          # value is empty
postfix::relays:             # value is ["relay01", "relay02"]
  - 'relay01'
  - 'relay02'

# hash or dict
postfix::aliases: {}         # value is empty
postfix::aliases:            # value is {"root"=>{"destination"=>"admin@example.com"} }
  root:
     destination: 'admin@example.com'

Data types

Automatic

---
my_data_types:
  boolean: true
  float: 3.14
  integer: 4
  string: somewords
  stringthatneedsquotes: "Some characters like % need to be encapsulated in quotes"
  stringifiedinteger:     '7'      # value is string '7', not integer 7
  stringifiedboolean:    'false'   # value is string 'false', not boolean false.

Set

---
specify_my_data_types:
  a_boolean: !!bool 'true'
  a_string:  !!str  true
  a_integer: !!num  '-1'

Output

$ hiera specify_my_data_types environment=testing
{"a_boolean"=>true, "a_string"=>"true", "a_integer"=>-1}

Anchors and Aliases

Limitations

  • Parse order dependance: Anchors must be defined first.
  • Merge is also depending on the order: Overrides must be after the merge.
  • Anchors and Alias must be in the same file. They are part of the Yaml specification, not of Hiera.
---
# The anchor, &apache_vhosts_defaults in this example
apache::vhosts::defaults: &apache_vhosts_defaults
  email:      'default_admin@example.com'
  servername: 'localhost'

# Anchors must be defined before they are referenced
# parser order matters
apache::vhosts:
  demo.example.com:
    <<: *apache_vhosts_defaults  # the <<: mean merge the hash
    port: '8088'
  test.example.com:
    <<: *apache_vhosts_defaults
    email: 'reza@example.com' # NOTE: parser order, this will overwrite the default
    port: '8080'
  prod.example.com:
    email: 'prod@example.com'  # NOTE: The merge happena ...

Facts and Functions

Referencing facts:

---
postfix::myorigin:    "%{::fqdn}"
postfix::relay:       "relay.%{::domain}"

Referencing functions

postfix::allowed_ips: "%{hiera('my_ips')}"

Using hiera within the Yaml file in this way allows to query other values of the yaml document as well:

---
setting1: value1
setting2: "%{hiera('setting1')}"

Output:

$ hiera setting1
value1
$ hiera setting2
value1

Multi-line blocks

---
multi_line1: |
   line1 first line
   line2 second line

multi_line2: >
   line1 first line
   line2 second line