bjones
bjones

Reputation: 23

Using variables in Puppet array two at a time

I have an each iteration in Puppet to install Perl module extensions:

$extensions_list = ["extension1",
                    "extension2",
                   ]
$extensions_list.each |$extls| {
  exec { $extls:
    path    => '/usr/local/bin/:/usr/bin/:/bin/',
    command => "wget http://search.cpan.org/CPAN/authors/id/B/BP/BPS/extension1-1.00.tar.gz",
  }
}

What I would like it to do as well is to take into account the version number, as in:

$extensions_list = ["extension1", "1.00",
                    "extension2", "2.00",
                   ]
$extensions_list.each |$extls| {
  exec { $extls:
    path    => '/usr/local/bin/:/usr/bin/:/bin/',
    command => "wget http://search.cpan.org/CPAN/authors/id/B/BP/BPS/extension1-1.00.tar.gz",
  }
}

So, I'd like it to be able to take the first two variables in the array to install the first extension and then the next two and install that and so on and so on as I add new extensions. That way I can just add the name and version number to my array and it will install them in turn.

Upvotes: 1

Views: 725

Answers (1)

John Bollinger
John Bollinger

Reputation: 180093

I have an each iteration in Puppet to install Perl module extensions:

Well, no, not exactly. You have an each operator to declare an Exec resource corresponding to each element of your array. Among potentially important distinctions from what you said is that the operation is evaluated during catalog building, so no actual installations are taking place at that time.

So, I'd like it to be able to take the first two variables in the array to install the first extension and then the next two and install that and so on

You could use the slice() function to split the array into an array of two-element arrays, and iterate over that. Consider, however, how much more natural it would be to use a hash instead of an array as the underlying data structure. Example:

$extensions_hash = {"extension1" => "1.00",
                    "extension2" => "2.00",
                   }
$extensions_hash.each |$extls, $extv| {
  exec { $extls:
    path    => '/usr/local/bin/:/usr/bin/:/bin/',
    command => "wget http://search.cpan.org/CPAN/authors/id/B/BP/BPS/$extls-$extv.tar.gz",
  }
}

Upvotes: 2

Related Questions