Problem with hiera in inline_template

10 views
Skip to first unread message

Alexander Sorochynskyi

unread,
Dec 19, 2019, 12:24:20 PM12/19/19
to Puppet Users
Hi.
I have some .yaml file:

iptables::test:
  ip:
    1.1.1.1  : 'adm-1'
    2.2.2.2  : 'adm-2'
    3.3.3.3  : 'adm-3'

And i want to parse this file in
inline_template.

I do:

$variable1 = hiera('iptables::test.ip') $variable2 = inline_template("<% @variable1.each do |key,value| %>Allow From <%=key %> #<%=value %>\n<% end -%>")

But get error:
Error 400 on SERVER: Could not find data item iptables::test.ip in any Hiera data file and no default supplied

Could you help me how parse my .yaml in inline_template. Thank you.

jcbollinger

unread,
Dec 20, 2019, 10:35:25 AM12/20/19
to Puppet Users


On Thursday, December 19, 2019 at 11:24:20 AM UTC-6, Alexander Sorochynskyi wrote:
Hi.
I have some .yaml file:

iptables::test:
  ip:
    1.1.1.1  : 'adm-1'
    2.2.2.2  : 'adm-2'
    3.3.3.3  : 'adm-3'

And i want to parse this file in
inline_template.

I do:

$variable1 = hiera('iptables::test.ip') $variable2 = inline_template("<% @variable1.each do |key,value| %>Allow From <%=key %> #<%=value %>\n<% end -%>")

But get error:
Error 400 on SERVER: Could not find data item iptables::test.ip in any Hiera data file and no default supplied


If you are on Puppet 4 or later, then you should be using the lookup() function instead of the deprecated hiera() family of functions.  On Puppet 5.5 and later (and perhaps on earlier 5.x releases) that has the particular advantage that lookup() is documented to support the dotted key / subkey notation you are trying to use, whereas hiera() is not documented to support that on any version of Puppet.  The error message you present is characteristic of this problem: the system is attempting to interpret the whole string "iptables::test.ip" as an Hiera key, instead of recognizing it as a separate key ("iptables::test") and subkey ("ip").

 
Could you help me how parse my .yaml in inline_template. Thank you.


On recent-enough Puppet, just switching from hiera() to lookup() will probably resolve the issue:

$variable1 = lookup('iptables::test.ip')

$variable2
= inline_template("<% @variable1.each do |key,value| %>Allow From <%=key %> #<%=value %>\n<% end -%>")


Alternatively, look up the top-level key, then extract the wanted data from it separately, for example:

$variable1 = lookup('iptables::test')
# or $variable1 = hiera('iptables::test')
$variable2
= inline_template("<% @variable1['ip'].each do |key,value| %>Allow From <%=key %> #<%=value %>\n<% end -%>")



John


Reply all
Reply to author
Forward
0 new messages