Iterate over a hash key/values in Puppet

允我心安 提交于 2019-12-04 23:34:20

问题


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.

I have this yaml file (hiera)

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

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.

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).

any guidance is appreciated.

Edit: 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.


回答1:


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


来源:https://stackoverflow.com/questions/24687808/iterate-over-a-hash-key-values-in-puppet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!