0

I have a pretty annoying error I am desperate to fix. I have a puppet module where certain things are kept in a yaml file (hiera) so that people could quickly edit just that part.

The structure is nested.

The template like this:

<%= @platforms[@platform]['users_allowed'][@host].class %>

renders to this:

Array

But when I try to iterate over it:

<% @platforms[@platform]['users_allowed'][@host].each do | pubkey | %>
<%= pubkey %>
<% end %>

puppet says:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Failed to parse template somemodule/templates/authorized_keys.erb:
Filepath: /etc/puppet/modules/somemodule/templates/authorized_keys.erb
Line: 1
Detail: undefined method `each' for nil:NilClass

How does it suddenly become nil and how do I iterate here?

I'm afraid I'm stuck here and not even sure in which direction I should search for solution.

Rewriting module without hiera or without nested structure is not an option in this one case.

Roman Grazhdan
  • 334
  • 3
  • 15
  • what version of puppet? – Zypher Feb 21 '14 at 18:34
  • It's 3.3.1. I belive the part of the problem is this (quoting puppetlabs docs): The lookup functions and the automatic parameter lookup always return the values of top-level keys in your Hiera data — they cannot descend into deeply nested data structures and return only a portion of them. To do this, you need to first store the whole structure as a variable, then index into the structure from your Puppet code or template. -- but it's still weird to me. It does see the type and it can render a template with the whole array but it can't index. – Roman Grazhdan Feb 23 '14 at 16:09

1 Answers1

2

I've tested on my puppet and the correct syntax that worked was:

<% @platforms[@platform]['users_allowed'][@host].each do | pubkey | -%>
  <%= pubkey %>
<% end -%>

I hope I helped.

PoLIVoX
  • 195
  • 1
  • 1
  • 6
  • What version of puppet do you run? I think omitting one pipe must not help and if it does I'm in trouble. I mean, in Ruby array.each do | i | is valid, so it should be valid in erb, too. – Roman Grazhdan Feb 23 '14 at 18:39
  • When I typed I forgot to type a |. I already fix. Sorry for the mistake. Answering your question: Puppet 3.4.3. I used as the documentation recommends: http://docs.puppetlabs.com/learning/templates.html – PoLIVoX Feb 24 '14 at 02:24