Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over a hash key/values in Puppet

Tags:

puppet

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.

like image 309
Dexter Legaspi Avatar asked Jul 10 '14 23:07

Dexter Legaspi


1 Answers

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:}
like image 181
Dexter Legaspi Avatar answered Nov 15 '22 11:11

Dexter Legaspi