I am starting to develop in puppet (ruby) and I have the following problem.
I have the current hash that I want to iterate in a template file.
$database_profile_hash =
{
cpu => {
governor => ondemand
energy_perf_bias => powersave
}
sysctl => {
kernel.sched.min.granularity.ns => 10000000
kernel.sched.wakeup.granularity.ns => 15000000
kernel.msgmnb => 6
}
...
}
And my current template is the following:
<% @database_profile_array.each do |arrayValue| %>
[<%= arrayValue %>]
<% database_profile_hash[arrayValue].each do |key,value| %>
<%= key %> <%= value %>
<% end %>
<% end %>
To iterate the array I am trying to use an array to store all the first level names and then use it to iterate the hash:
$database_profile_array = [cpu,sysctl,...]
But I am not able to make it work and I am looking for an exit like this:
[cpu]
governor=ondemand
energy_perf_bias=powersave
[sysctl]
vm.laptop_mode=5
vm.dirty_writeback_centisecs=1500
kernel.nmi_watchdog=0
What I am doing wrong in the template? There is a way to pass the content of the variable "arrayValue" to iterate the hash?
Thanks a lot in advance...
Your template looks fine for the most part, but few things here. First off, you may not be able to use periods for your variable names, but more importantly, don't forget commas to separate key/value pairs:
$database_profile_array = [cpu,sysctl]
$database_profile_hash =
{
cpu => {
governor => ondemand,
energy_perf_bias => powersave,
},
sysctl => {
kernel_sched_min_granularity_ns => 10000000,
kernel_sched_wakeup_granularity_ns => 15000000,
kernel_msgmnb => 6,
}
}
In your template, you're forgetting the equal signs, and you also might want to omit new line characters for each iteration by using -%>
:
<% @database_profile_array.each do |arrayValue| -%>
[<%= arrayValue %>]
<% database_profile_hash[arrayValue].each do |key,value| -%>
<%= key %>=<%= value %>
<% end %>
<% end -%>
Edit: OP, note the comments others have left. If the iteration sequence doesn't matter, you don't need the separate array $database_profile_array
to reference the keys, rather you can iterate the hash directly:
<% @database_profile_hash.each do |key, hash| -%>
[<%= key %>]
<% hash.each do |key,value| -%>
<%= key %>=<%= value %>
<% end %>
<% end -%>
Also, the -%>
erb puppet trim tag is documented here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With