puppet在循环中添加数组元素

AzH*_*ofi 3 arrays loops puppet

我想要这样的东西:

$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,
}
Run Code Online (Sandbox Code Playgroud)

现在因为Puppet的变量范围而不可能.如何通过嵌套循环收集数组中的某些变量?

Mat*_*ard 9

你接近你的尝试,但你使用了错误的lambda类型.为了避免Puppet变量在同一范围内不可变的两个事实导致的问题,并且如果在lambda中定义,也不能在lambda范围之外使用,则必须使用rvalue lambda https://en.wikipedia.org/ wiki/Value_(computer_science)#R-values_and_addresses.我使用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"
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我做了notify { $site_developer_base: }这个输出:

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
Run Code Online (Sandbox Code Playgroud)

证明$site_developer_base有你想要的数组.

  • 很好用`map()`.这种事情在Puppet中过去很难. (2认同)