在 Puppet 中迭代哈希键/值 [英] Iterate over a hash key/values in Puppet

查看:55
本文介绍了在 Puppet 中迭代哈希键/值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Puppet 来更新 ASP.NET web.config 中的任意 appsettings 列表(用于部署目的),但我处于两难境地,主要是因为我是 Puppet 中的真正 n00b.

I'm dabbling with Puppet to update an arbitrary list of appsettings in an ASP.NET web.config (for deployment purpose) and I'm in a dilemma, mostly because I'm a real n00b in puppet.

我有这个 yaml 文件 (hiera)

I have this yaml file (hiera)

---
appSettings:
  setting1: "hello"
  setting2: "world!"
  setting3: "lalala"

setting[x] 的数量可以任意跨越(一个 appSetting),我想循环遍历哈希键/值来更新相应的 appSetting/add在 web.config 中(使用 exec 和 powershell)问题是我搜索了很多关于如何迭代 keysvalues.

the number of setting[x] can span arbitrarily (one appSetting) and I would like to loop through the hash keys/value to update the corresponding appSetting/add in the web.config (using exec with powershell) the problem is i've searched high and low on how to iterate on keys and values.

我遇到了 create_resources,这当然会使用一组预先确定的键来迭代散列的散列.同样,清单中的键名是未知的(因此迭代键/值对).

I came across create_resources and this of course iterates through a hash of hash with a pre-determined set of keys. again, the key names are not known within the manifest (hence iterating the key/value pairs).

感谢任何指导.

看起来有一个 keys() 函数,我可以在哈希上使用并迭代它,然后使用 hiera_hash('appSettings') 来获取哈希并迭代值.

looks like there is a keys() function i can use over the hash and iterate over that then use hiera_hash('appSettings') to get the hash and iterate through the values.

推荐答案

好的,我刚刚确认了您可以在清单中执行的操作:

ok i just confirmed that what you can do in your manifest:

define updateAppSetting {
    # get the hashes again because outside vars aren't visible here
    $appSettings = hiera_hash('appSettings')

    # $name is the key $appsettingValue is the value
    $appsettingValue = $appSettings[$name]

    # update the web.config here!
}

$appSettings = hiera_hash('appSettings')    

# the keys() function returns the array of hash keys
$appSettingKeys = keys($appSettings)

# iterate through each appSetting key
updateAppSetting{$appSettingKeys:}

这篇关于在 Puppet 中迭代哈希键/值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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