Access hash and array elements using a key.subkey notation

Access hash and array members in Hiera using a key.subkey notation.

You can access hash and array elements when doing the following things:

  • Interpolating variables into hiera.yaml or a data file. Many of the most commonly used variables, for example facts and trusted, are deeply nested data structures.

  • Using the lookup function or the puppet lookup command. If the value of lookup('some_key') is a hash or array, look up a single member of it by using lookup('some_key.subkey').

  • Using interpolation functions that do Hiera lookups, for example lookup and alias.

To access a single member of an array or hash:

  1. Use the name of the value followed by a period (.) and a subkey.

    • If the value is an array, the subkey must be an integer, for example: users.0 returns the first entry in the users array.

    • If the value is a hash, the subkey must be the name of a key in that hash, for example, facts.os.

    • To access values in nested data structures, you can chain subkeys together. For example, because the value of facts.system_uptime is a hash, you can access its hours key with facts.system_uptime.hours.

    Example:

    To look up the value of home in this data:

    accounts::users:
      ubuntu:
        home: '/var/local/home/ubuntu'

    You would use the following lookup command:

    lookup('accounts::users.ubuntu.home')