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

@Volcane on #puppet suggested:

instead of doing alias_ips[number] try: alias_ips.fetch(number, [])
which if thats the case will return [] instead of nil

Bizarely, this fixes it!

@jessereynolds
Copy link
Author

Adding debugging as follows:

<%= ips.class.instance_methods(false) %>
<%= ips.class.instance_methods(true) %>

outputs:

** [out :: bpsyd07-pmdh-collectd-02.inf.bulletproof.net] + insertsortfillinclude?size&uniq!atreplacechoicezipreject!shiftcompactto_shashtransposecycle_indexcollect+pushcountuniqdelete_iftaguriproductdropdeleteindexesselectmap!-eql?fetchlengthassocvalues_atpermutation[]taguri=takeinspectsort!shuffle!unshift[]=|find_indexslice!eachempty?<<to_aryfrozen?clearyaml_initializerindexshufflemapreverse_eachpackreverse!to_apopflatten!firstdrop_whileto_zamlconcatrejectjoinindicesslicenitems<=>rassoclastcompact!delete_at==combinationtake_whileto_yamleach_indexcollect!reverseflatten
_ [out :: bpsyd07-pmdh-collectd-02.inf.bulletproof.net] + zipproductcompact!find_indexindicesmeta_evalminmaxflattenfetchmethodssendreplaceempty?group_bycleartaintto_enuminstance_variable_defined?to_plisttaguritakefind_allmeta_defmin_bytaguri=entrieslastsingleton_methodsinstance_evalfirstfillsort!enum_fornil?to_yaml_styletake_whileselectdecode64flatten!meta_undefmax_byb64encodenitemsprotected_methodsinstance_execsortpushtainted?to_yamlinclude?assocjoinpackuntaintcollect!each_sliceinstance_of?dropequal?rejectminmax_byhashclass_defprivate_methodsall?sort_bypoprassocencode64kind_of?enum_slicemap!freezeyaml_initializedrop_whileto_yaml_propertieseql?shuffle!collectvalues_atassert_thatidsliceshufflegrepshiftany?public_methodsuniq&is_a?each_conscycleyaml_property_mungemapdelete_atmember?taptypereturninginserteach_with_index+choicecountunshiftone?instance_variables__id__frozen?-to_setto_aenum_consrespond_to?zamlized_class_namedisplayeachinjectdelete_ifmethodslice!classreverselengthenum_with_indexpermutationto_zamleach_index<=>none?uniq!instance_variable_getfind==send===mincompactextendto_sto_aryindexsizereduce|clonereverse_eachcombinationsave_plist=~instance_variable_setdetectindexesmaxdecode_bsingleton_classatreject!inspect[]reverse!daemonizerindexpartitiondeleteto_psontranspose[]=concatdupobject_id<<

@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