木偶在循环中添加数组元素 [英] puppet adding array elements in a loop

查看:65
本文介绍了木偶在循环中添加数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要这样的东西:

$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,
}

现在,由于Puppet的范围可变,这是不可能的。我如何通过嵌套循环在数组中收集一些变量?

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

推荐答案

您的尝试虽然很接近,但是您使用的是错误类型的Lambda。为避免因以下两个事实而导致的问题:Puppet变量在同一范围内是不可变的,并且如果在lambda中定义,也不能在lambda范围之外使用,则必须使用右值lambda https://en.wikipedia.org/wiki/Value_(computer_science)#R-values_and_addresses 。我使用右值lambda map https://docs.puppet.com/puppet/latest/function.html#map

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"
  }
}

如果我执行通知{$ 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

证明 $ site_developer_base 具有所需的数组。

这篇关于木偶在循环中添加数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆