Jira (PUP-9714) Add support for nested hiera interpolation

13 views
Skip to first unread message

Nick Bertrand (JIRA)

unread,
May 21, 2019, 6:27:04 PM5/21/19
to puppe...@googlegroups.com
Nick Bertrand created an issue
 
Puppet / New Feature PUP-9714
Add support for nested hiera interpolation
Issue Type: New Feature New Feature
Assignee: Unassigned
Created: 2019/05/21 3:26 PM
Priority: Normal Normal
Reporter: Nick Bertrand

This commit adds support for two levels of nested hiera interpolations. For example, given a fact called hostgroup with value group1, the following hiera data would have local_users::add::users.root.password set to $6$....

---
group1_root_password: $6$....
local_users::add::users:
 root:
   password: '%{lookup("%{facts.hostgroup}_root_password")}'

If this is useful for merging, I would be glad to update any necessary documentation.

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v7.7.1#77002-sha1:e75ca93)
Atlassian logo

Josh Cooper (JIRA)

unread,
May 28, 2019, 1:25:03 PM5/28/19
to puppe...@googlegroups.com
Josh Cooper updated an issue
Change By: Josh Cooper
Sub-team: Language

Josh Cooper (JIRA)

unread,
May 28, 2019, 1:25:12 PM5/28/19
to puppe...@googlegroups.com

Henrik Lindberg (JIRA)

unread,
May 29, 2019, 11:23:03 AM5/29/19
to puppe...@googlegroups.com
Henrik Lindberg commented on New Feature PUP-9714
 
Re: Add support for nested hiera interpolation

Here is an example of how the same can be achieved with hiera 5 and a simple backend function written in Puppet Language.

# Do whatever is needed for a special key here that interpolates a value to form another key that is looked up.
# This example is hardcoded for one key only, but could easily read data from a file, support multiple keys etc.
#
function mymodule::special_key(Variant[String, Numeric] $key, Hash  $options, Puppet::LookupContext  $context) {
  unless $key == 'hostgroup_specific_root_password' {
    $context.not_found()  # will return from this function
  }
  lookup("${facts[hostgroup]}_root_password")
}

Then add that function to hiera.yaml as a lookup_key kind of function, and use hiera regular interpolation in the data to get that key into the value of a hash. Using the example in the description this is what is in the data where the special value is wanted:

---
group1_root_password: $6$....
local_users::add::users:
 root:
   password: '%{alias("hostgroup_specific_root_password")}'

Documentation for lookup_key kind of backend functions is here: https://puppet.com/docs/puppet/4.9/hiera_custom_lookup_key.html#calling-conventions-for-lookupkey-functions

Henrik Lindberg (JIRA)

unread,
May 29, 2019, 11:33:03 AM5/29/19
to puppe...@googlegroups.com

The above could be turned into a "key mapper" function. A user of the function would specify "path" to a data file, and the data file would contain the key mapping:

---
hostgroup_specific_root_password: %{facts.hostgroup}_root_password'

The function would read the data file given to the function in options[path], using a read_yaml function. It can then cache this result using the $context cache methods. On a lookup, it gets the data from the cache, if the key being looked up is present (e.g. 'hostgroup_specific_root_password'), it gets the value and calls $context.interpolate to interpolate it. This forms the new key, which it then uses in a call to lookup - the returned value from lookup is returned from the function.

This generalized version would only be a couple of lines longer than the first example.

Henrik Lindberg (JIRA)

unread,
May 29, 2019, 11:39:03 AM5/29/19
to puppe...@googlegroups.com

I would much rather see that the requested feature is solved via use of a lookup_key kind of function than modifying hiera's interpolation to do nested interpolation.

Robert August Vincent II (JIRA)

unread,
Jun 3, 2019, 1:18:03 PM6/3/19
to puppe...@googlegroups.com

There needs to be a docs change, as well.

Maggie Dreyer (Jira)

unread,
Dec 2, 2020, 2:42:03 PM12/2/20
to puppe...@googlegroups.com
Maggie Dreyer commented on New Feature PUP-9714

In general, we are not prioritizing any Hiera features for the time being. If that changes, we can revisit this.

This message was sent by Atlassian Jira (v8.5.2#805002-sha1:a66f935)
Atlassian logo
Reply all
Reply to author
Forward
0 new messages