Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

puppet adding array elements in a loop

I want something like this:

$ssl_domains = ['dev.mydomain.com']

['admin', 'api', 'web'].each |$site| {
  ['tom', 'jeff', 'harry'].each |$developer| {
    $ssl_domains << "$site.$developer.dev.mydomain.com"
  }
}

letsencrypt::certonly { 'dev-cert':
  domains     => $ssl_domains,
  plugin      => 'apache',
  manage_cron => true,
}

now it is impossible because of Puppet's variable scoping. How can I collect some variables in an array through nested loops?

like image 325
AzHofi Avatar asked Dec 08 '16 14:12

AzHofi


People also ask

Can you use a for loop for an array?

A for loop can be used to access every element of an array. The array begins at zero, and the array property length is used to set the loop end.


1 Answers

You were close with your attempt, but you were using the wrong type of lambda. To avoid the issues resulting from the two facts that Puppet variables are immutable within the same scope and also cannot be used outside of a lambda scope if defined within a lambda, you must use an rvalue lambda https://en.wikipedia.org/wiki/Value_(computer_science)#R-values_and_addresses. I solved your problem using the rvalue lambda map https://docs.puppet.com/puppet/latest/function.html#map.

$site_developer_base = ['admin', 'api', 'web'].map |$site| {
  $developer_base = ['tom', 'jeff', 'harry'].map |$developer| {
    "${site}.${developer}.dev.mydomain.com"
  }
}

If I do a notify { $site_developer_base: } this outputs:

Notice: admin.tom.dev.mydomain.com
Notice: admin.jeff.dev.mydomain.com
Notice: admin.harry.dev.mydomain.com
Notice: api.tom.dev.mydomain.com
Notice: api.jeff.dev.mydomain.com
Notice: api.harry.dev.mydomain.com
Notice: web.tom.dev.mydomain.com
Notice: web.jeff.dev.mydomain.com
Notice: web.harry.dev.mydomain.com

proving that $site_developer_base has the array that you want.

like image 134
Matt Schuchard Avatar answered Oct 04 '22 02:10

Matt Schuchard