I have got some .yaml hiera file with:

  ip:  : 'adm-1'  : 'adm-2'  : 'adm-3'

And i want to parse this file in inline_template. I write:

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

But get an error:

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

There's a problem with either your data structure or your logic, or both. I am not sure I have enough here to sort out which.

The first issue I see is your hiera() lookup function cannot lookup the nest ip hash directly. Your Hiera key is just iptables::test. You can get the full value of that via lookup and parse it further if needed.

$variable1 = hiera('iptables::test')

If you don't need the nested ip hash, your inline_template() works as written. Your data structure would just be a single hash.

iptables::test: adm-1 adm-2 adm-3

If you need the nested hash, then you need a nested loop.

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

Putting it together to demonstrate:

$ cat test.pp 
$variable1 = {
  ip  => {
    '' => 'adm-1',
    '' => 'adm-2',
    '' => 'adm-3',

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

$ puppet apply test.pp
Notice: Scope(Class[main]): Allow From #adm-1
Allow From #adm-2
Allow From #adm-3

Notice: Compiled catalog for localhost in environment production in 0.02 seconds
Notice: Applied catalog in 0.01 seconds

My test here did not use Hiera, because Hiera is just a way to bring data from outside of a Puppet class. I wanted to demonstrate this way because it will be easier for you to isolate your problem.

| improve this answer | |
  • Thank you for reply. Problem was in old version puppet. – perrfect Jan 13 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.