Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Could someone please help me understand how I could loop through the following nodes definition in my templates file?

ip::addr{ "as2s67v":
    ip0 => "192.168.30.0",
    pre0 => "/24",
    ip1 => "172.45.35.0",
    pre1 => "/16",
    ip2 => "145.88.88.0",
    pre2 => "/24",
}

I'd like to print out the above using my template file, however I can't be certain of how many entries will exist, I want to end up with a file like so:

server: as2s67v
ipaddress0 = "192.168.30.0"
prefix0 = "24"
ipaddress1 = "172.45.35.0"
prefix1 = "16"
ipaddress2 = "145.88.88.0"
prefix2 = "24"

How could I go about achieving this?

thanks Dan

share|improve this question

Not exactly what you're looking for, but it cuts down on having to keep multiple variables in sync:

site.pp:

class { 'ip::addr':
  name  => "as2s67v",
  cidrs => [ "192.168.30.0/24", "172.45.35.0/16", "145.88.88.0/24" ]
}

modules/ip/addr.pp:

class ip::addr ($cidrs) {
  file { "/tmp/$name":
    content => template("ip/template.pp")
  }
}

template.pp:

server: <%= name %>
<% for idx in (0..(cidrs.length-1)) -%>
ipaddress<%= idx %> = "<%= cidrs[idx].split("/")[0] %>"
prefix<%= idx %> = "<%= cidrs[idx].split("/")[1] %>
<% end -%>

References:

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.