Created
October 3, 2018 14:51
-
-
Save GeoffWilliams/d7ff69b2e512b1207e9e676db1dbac14 to your computer and use it in GitHub Desktop.
Example of how to use Puppet, iteration and functions to figure out what to look up from hiera, look it up and then collect the results in a file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# big parameters for testcase purposes - normally default to empty hash and lookup from hiera | |
class profiles::myapp( | |
Hash $packages = { | |
"vb" => { | |
description => "the best beer in australia", | |
}, | |
"oldgnarlyhen" => { | |
description => "for hipsters", | |
} | |
}, | |
Array[String] $hives = ["app","db","kms","cea","lbs"], | |
String $config_base = "myapp", | |
) { | |
# install required component packages | |
$json_hash = $packages.map |$key, $opts| { | |
file { "/installed_${key}": | |
ensure => present, | |
}; | |
exec { "/bin/cat /tmp/data.json > /tmp/data.json.${key}": | |
require => File['/tmp/data.json'], | |
}; | |
# must be last! | |
$hives.map |$hive| { | |
# interpolate the key to lookup in hiera eg `myapp::widget::app_settings` | |
$lookup_key = "${config_base}::${key}::${hive}_settings"; | |
{$lookup_key => lookup($lookup_key)} | |
} | |
}.reduce |$memo, $x| { | |
# collect all the arrays and make a hash | |
$memo + $x | |
} | |
file{"/tmp/data.json": | |
ensure => present, | |
content => to_json_pretty($json_hash), | |
} | |
} | |
Results in: | |
[root@agent /]# puppet apply /shared/testa.pp | |
Notice: Compiled catalog for agent.localdomain in environment production in 0.13 seconds | |
Notice: /Stage[main]/Profiles::Myapp/File[/tmp/data.json]/ensure: defined content as '{md5}eb6fc42f8be0360e1fe2c41da0c1b3b7' | |
Notice: /Stage[main]/Profiles::Myapp/Exec[/bin/cat /tmp/data.json > /tmp/data.json.vb]/returns: executed successfully | |
Notice: /Stage[main]/Profiles::Myapp/Exec[/bin/cat /tmp/data.json > /tmp/data.json.oldgnarlyhen]/returns: executed successfully | |
Notice: Applied catalog in 0.05 seconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment