5
votes

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
The approach sounds somewhat abusive. Wouldn't it be more economic to write the key/value pairs to a file on the agent and have a single monolithic PowerShell script do all the editing in one go? - Felix Frank
@FelixFrank sorry i don't quite get why it's "abusive." the PowerShell script will update the web.config one app setting at a time. can you elaborate on what a better approach would be? - Dexter Legaspi
ideally you could create a template that emits the full contents for your file. Barring that, it would be desirable to manage things in just two resources - one file with the key/value pairs as found in Hiera, and one exec with a slightly more powerful script to do the job of all your current execs and apply all the key/value pairs. You'd likely need an additional script to determine if there is work to do on the target file, to form your onlyif condition. - Depending on how important performance is, this may be nitpicking - Felix Frank
@FelixFrank oh i see...i originally wanted to use augeas but it's not available in Windows...I came across templating features of puppet but it didn't occur to me that I can use it...thanks for the pointers. - Dexter Legaspi

1 Answers

8
votes

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