1

I have got some .yaml hiera file with:

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 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
Aaron Copley
  • 12,525
  • 5
  • 47
  • 68
perrfect
  • 65
  • 1
  • 7

1 Answers1

1

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:
  1.1.1.1: adm-1
  2.2.2.2: adm-2
  3.3.3.3: 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  => {
    '1.1.1.1' => 'adm-1',
    '2.2.2.2' => 'adm-2',
    '3.3.3.3' => 'adm-3',
  },
}

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

notice($variable2)
$ puppet apply test.pp
...
Notice: Scope(Class[main]): Allow From 1.1.1.1 #adm-1
Allow From 2.2.2.2 #adm-2
Allow From 3.3.3.3 #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.

Aaron Copley
  • 12,525
  • 5
  • 47
  • 68