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

I'm passing a hash to a define like so:

nodes.pp

    net::addr { 'routes':
    ipr => {
        ipr1 => {
            address => '192.168.34.38',
            netmask => '255.255.255.0',
            gateway => '192.168.34.1',
            dev     => 'eth3',
        },
        ipr22 => {
            address => '10.1.5.33',
            netmask => '255.255.255.0',
            gateway => '10.1.5.1',
            dev     => 'eth2',
        },
  }
  }

addr.pp

define net::addr (
    $routes={},
)
{
    if ($num != "") {
     $iname=${routes[route${num}][dev]}
     $num+1
    }
    else{
     $num=0;
    }
}

What I want to do is define a variable called iname and set it to the dev under each hash element, so on iteration 1 I want dev to be set to eth3, which corresponds to the "ipr1" element, on the next iteration I was iname set to eth2 and so on.

I'm struggling to do this as everytime my num variable keeps getting undefined and then ends up being set to 0, can someone please suggest a way round this or a more intelligent solution?

share|improve this question

As of Puppet version 3.3, the Puppet DSL language does not support iteration.

You should check out the create_resources() function from Puppet Labs:
http://docs.puppetlabs.com/references/latest/function.html#createresources

Example

# A hash of user resources:
$myusers = {
  'nick' => { uid    => '1330',
              group  => allstaff,
              groups => ['developers', 'operations', 'release'], }
  'dan'  => { uid    => '1308',
              group  => allstaff,
              groups => ['developers', 'prosvc', 'release'], }
}

create_resources(user, $myusers)

However, as of Puppet 4.0, the Future Parser now supports iteration with Lambdas:

$data = ['a','b','c']

$data.each |Integer $index, String $value| {
    notice("${index} = ${value}")
}
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.