Skip to content

Instantly share code, notes, and snippets.

@jessereynolds
Last active December 18, 2015 21:48
Show Gist options
  • Save jessereynolds/5849704 to your computer and use it in GitHub Desktop.
Save jessereynolds/5849704 to your computer and use it in GitHub Desktop.
puppet - accessing items of an array within a hash from an erb template
I'm having trouble iterating over an array, that's been retrieved from a hash. Here's the hash definition in the node manifest:
$alias_ips = {
24 => [
'10.254.16.82/24',
],
}
This hash is passed as a parameter to a named type, which in turn processes an erb template
as part of a File definition. The erb file contains:
<% unless alias_ips == :undef -%>
<% ips = alias_ips[number] -%>
<%= ips.inspect %>
<%= ips.class %>
<% end -%>
The above executes and puts the following in the resulting file:
["10.254.16.82/24"]
Array
Now, if I try and iterate through each item in this array like this:
<% unless alias_ips == :undef -%>
<% ips = alias_ips[number] -%>
<%= ips.inspect %>
<%= ips.class %>
<% ips.each do |ip| -%>
<%= ip %>
<% end -%>
<% end -%>
then I get the following error trying to apply the manifest:
err: Could not retrieve catalog from remote server: Error 400 on SERVER: Failed to parse
template network/types/bonded_vlan/interfaces_precise.erb: undefined method `each' for
nil:NilClass at /etc/puppet/modules/network/manifests/type/bonded_vlan.pp:21 on node foo.example.com
So, given its getting an Array for the variable 'ips', how come .each doesn't work?
@jessereynolds
Copy link
Author

Trying to get ips.size yields this error:

** [out :: bpsyd07-pmdh-collectd-02.inf.bulletproof.net] err: Could not retrieve catalog from remote server: Error 400 on SERVER: Failed to parse template network/types/bonded_vlan/interfaces_precise.erb: undefined method `size' for nil:NilClass at /etc/puppet/modules/network/manifests/type/bonded_vlan.pp:21 on node bpsyd07-pmdh-collectd-02.inf.bulletproof.net

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment