Is there any way to iterate or access all the variables passed by the template resource in a ERB template?
Example: Let say I have this template definition:
template "path/name" do
Can I access all of them doing something similar to the below instead of using the variable names individually?
@variables.each do |var|
var which is array or hash and iterate through that
It is possible, but not in a way you won’t regret later. If you really must know, ruby has an
instance_variables method that will give you the list, and an
instance_variable_get method that lets you access them without using literals. That said, templates have some instance variables that Chef Client sets behind the scenes for bookkeeping, and the names and existence or non-existence of those can change at any time. Plus, what you’re suggesting would break if you ever added a different variable that wasn’t conceptually part of the list.
Per the previous reply, the best thing to do is pass your variable in as a collection of some sort and then iterate over that.
Great feedback, thank you guys.
Yeah I should probably go with the collection option.