puppet and array loop

24,303

Solution 1

Any particular reason you need to use that data structure? That will get unreadable very quickly, and would be very easy to break things by accidentally adding or removing an element from one of the arrays and not the others. How about something like this instead?

$servers = [
  {
    name => 'felix',
    ip   => '192.168.43.5',
    env  => 'prod',
  },
  {
    name => 'washington',
    ip   => '192.168.43.11',
    env  => 'uat',
  },
]

Then it's much simpler to work with in the erb:

<% servers.each do |server| -%>
  <%= server['name'] %> <%= server['ip'] %> <%= server['env'] %>
<% end -%>

But, if you're stuck with your current data structure, then this should do it..

<% (1..servername.length).each do |i| -%>

Solution 2

Shane Madden's answer is great but for I think there is a '@' missing:

instead of

<% servers.each do |server| -%>

one should use

<% @servers.each do |server| -%>

can someone with higher reputation please comment that in the original answer and/or acknowledge that so my "answer" could be deleted

This is also how it is documented on puppet cookbook third edition

Thanks, Matt

Share:
24,303

Related videos on Youtube

Dan
Author by

Dan

Updated on September 18, 2022

Comments

  • Dan
    Dan over 1 year

    Hi I'd like some help on how to code this in erb in puppet, basically I have

    server::actionhost { 'details':
        servername[ 'felix', 'washington', ],
        ipa [ '192.168.43.5', '192.168.43.11', ],
        enviro [ 'prod', 'uat', ],
    }
    

    I now want to print this out to a file with each respective element from each array in one line, i.e the output from the template file in my class should be like:

    felix 192.168.43.5 prod
    washington 192.168.43.11 uat
    

    When I attempted this I wrote the following code in my template file:

    <% servername.each do |name| -%>
        <% ipa.each do |ip| -%>
            <% enviro.each do |env| -%>
                <%= name %> <%= ip %> <%= env %>
            <% end -%>
        <% end -%>
    <% end -%>
    

    but what I get is multiple recursive prints instead of a print from each array and then move to the next array element.

    I'm thinking of a for loop but unsure how to get the array length as the argument to the for loop, Would appreciate some guidance on how to accomplish the correct output?

    I tried doing something like this but it fails with errors on the puppet run?

    <% for id in servername.length %>
        <%= servername[id] %> <%= ipa[id] %> <%= enviro[id] %>
    <% end -%>
    

    Thanks Dan

  • Dan
    Dan over 11 years
    That's brilliant. Thank you so much. I'm going to change my structure to your suggestion. Dan
  • Dan
    Dan over 11 years
    btw how do I define the $servers in my nodes.pp? Thanks
  • ravi yarlagadda
    ravi yarlagadda over 11 years
    @Dan Are you using that variable in just one particular node (maybe a provisioning node of some kind?) or in many or all nodes?
  • chandank
    chandank about 11 years
    While I am using the loop, I am getting error undefined method `each' for :undef:Symbol.
  • ravi yarlagadda
    ravi yarlagadda about 11 years
    @chandank That probably means that the variable you're looping over isn't in the current scope. Can you provide more specific detail about your manifests and template? A new question would probably be the best place for that.
  • chandank
    chandank about 11 years
    I am terrible with typing, some typo errors were causing issue. Thanks for help.
  • Nate Lampton
    Nate Lampton about 5 years
    Per Matt V in another answer, could you add @ before "servers" in this answer so that the variable can be found? Without it you get an evaluation error.