The Puppet Labs Issue Tracker has Moved: https://tickets.puppetlabs.com

This issue tracker is now in read-only archive mode and automatic ticket export has been disabled. Redmine users will need to create a new JIRA account to file tickets using https://tickets.puppetlabs.com. See the following page for information on filing tickets with JIRA:

Feature #21367

Add support for a hiera variable syntax which interpolates data by performing a hiera lookup

Added by Jesse Hathaway almost 3 years ago. Updated over 2 years ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:-
Target version:1.3.0
Keywords: Affected Hiera Version:
Branch:https://github.com/puppetlabs/hiera/pull/152

We've Moved!

Ticket tracking is now hosted in JIRA: https://tickets.puppetlabs.com


Description

It would be valuable to support interpolating hiera data in a similar fashion to how scope interpolation functions in hiera. This example uses the ^{foo} syntax to differentiate hiera lookups from scope lookups, %{foo}.

ips.yaml


potto01_ip: 10.10.1.52

pott01.yaml


firewall_rules:
  - "0.0.0.0:22:^{pott01_ip}"

When evaluating the string, "0.0.0.0:22:^{pott01_ip}" hiera would lookup the value for pott01_ip in hiera and interpolate that into the string, the result being, "0.0.0.0:22:10.10.1.52". This avoids having to repeatedly perform this type of lookup logic in puppet and allows you to use your hiera data from within hiera itself.


Related issues

Related to Hiera - Feature #23301: Cannot lookup non-string values Accepted

History

#1 Updated by Jesse Hathaway almost 3 years ago

Pull request which adds this feature:

https://github.com/puppetlabs/hiera/pull/137

#2 Updated by Adrien Thebo over 2 years ago

  • Status changed from Unreviewed to Merged - Pending Release
  • Target version set to 1.3.0
  • Branch set to https://github.com/puppetlabs/hiera/pull/152

Merged into master in f347bbe; this should be released in 1.3.0.

#3 Updated by eric sorenson over 2 years ago

Note the functionality that actually got merged differs from Jesse’s original description, after some iteration (hah!) on the pull request and mailing list. Henrik described the current functionality as such:

The support is based on calling lookup functions in the interpolation. Two such functions are supported: * hiera('hiera_key') * scope('variable_name') They are used in hiera interpolation like this: mykey: "I include %{hiera('otherkey')}" otherkey: "the other key" When looking up the key 'mykey', the result is "I include the other key". The key argument in the function must be surrounded by ' or ". The scope function is basically the same as the regular variable interpolation, but it performs an extra lookup in the given 'extra data' that can be supplied to hiera when doing lookup if the lookup in the regular scope did not produce a value. (Someone else may have a better explanation / examples of usage). The implementation checks for endless recursion and issues an error if an attempt is made to interpolate an already interpolated key. Note that it is sensitive wrt. whitespace, users should not have any whitepsace in the interpolation function. (e.g. this will not work "I include %{ hiera( 'otherkey' ) }")

(from https://groups.google.com/d/msg/puppet-dev/TBaPVZHgPs8/9Tx_WV3k-j0J )

#4 Updated by Nick Fagerlund over 2 years ago

As implemented, this barfs violently when the hiera(‘something’) lookup finds a non-string value. (Boolean, array, hash, etc.)

apache::version: "2.2.21"
is_false::is_false: false
arbitrary_hash:
  value1: something
  value2: "something else"
  value3: "Value of apache version is %{hiera(\"apache::version\")}"
  value4: %{hiera("is_false::is_false")}

This will fail with:

nick@wren:~$ hiera arbitrary_hash
/Users/nick/src/hiera/lib/hiera/interpolate.rb:44:in `sub': can't convert false into String (TypeError)
    from /Users/nick/src/hiera/lib/hiera/interpolate.rb:44:in `hiera_interpolate'
    from /Users/nick/src/hiera/lib/hiera/interpolate.rb:13:in `send'
    from /Users/nick/src/hiera/lib/hiera/interpolate.rb:13:in `interpolate'
    from /Users/nick/src/hiera/lib/hiera/recursive_guard.rb:16:in `check'
    from /Users/nick/src/hiera/lib/hiera/interpolate.rb:11:in `interpolate'
    from /Users/nick/src/hiera/lib/hiera/backend.rb:93:in `parse_string'
    from /Users/nick/src/hiera/lib/hiera/backend.rb:104:in `parse_answer'
    from /Users/nick/src/hiera/lib/hiera/backend.rb:109:in `parse_answer'
    from /Users/nick/src/hiera/lib/hiera/backend.rb:107:in `each_pair'
    from /Users/nick/src/hiera/lib/hiera/backend.rb:107:in `parse_answer'
    from /Users/nick/src/hiera/lib/hiera/backend/yaml_backend.rb:71:in `lookup'
    from /Users/nick/src/hiera/lib/hiera/backend.rb:73:in `datasources'
    from /Users/nick/src/hiera/lib/hiera/backend.rb:71:in `map'
    from /Users/nick/src/hiera/lib/hiera/backend.rb:71:in `datasources'
    from /Users/nick/src/hiera/lib/hiera/backend/yaml_backend.rb:40:in `lookup'
    from /Users/nick/src/hiera/lib/hiera/backend.rb:174:in `lookup'
    from /Users/nick/src/hiera/lib/hiera/backend.rb:171:in `each'
    from /Users/nick/src/hiera/lib/hiera/backend.rb:171:in `lookup'
    from /Users/nick/src/hiera/lib/hiera.rb:60:in `lookup'
    from /Users/nick/bin/hiera:221

Is this by design? Given that unquoted interpolation tokens are illegal under psych, I could kind of see it going either way.

#5 Updated by Melissa Stone over 2 years ago

  • Status changed from Merged - Pending Release to Closed

Released in Hiera 1.3.0-rc2

Also available in: Atom PDF