Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
this is an example usage of iterators in puppet 3.4
# challenge: write an ini file from hiera input
# $cache_cluster_instances = [
# { host => 'cache-a', weight => 1 },
# { host => 'cache-b', weight => 0.5 },
# { host => 'cache-c', weight => 0.5 },
# ]
$cache_cluster_instances = hiera('cache_cluster_instances')
$cache_cluster_instances.each |$i, $cache_cluster_instance| {
Ini_Setting {
ensure => present,
path => '/tmp/myapp.ini',
}
ini_setting { "cache_instance${i}_host" :
section => 'cache_cluster',
setting => "${i}.host",
value => $cache_cluster_instance['host'],
}
ini_setting { "cache_instance${i}_weight" :
section => 'cache_cluster',
setting => "${i}.weight",
value => $cache_cluster_instance['weight'],
}
}
###
# "with create_resources":
###
# $cache_cluster_instances = [
# { host => 'cache-a', weight => 1 },
# { host => 'cache-b', weight => 0.5 },
# { host => 'cache-c', weight => 0.5 },
# ]
$cache_cluster_instances = hiera('cache_cluster_instances')
cache_cluster_instances_to_ini('my cache cluster', $cache_cluster_instances)
define cache_cluster_instances_to_ini(
$instances
) {
$ini_settings_defaults = {
ensure => present,
path => '/tmp/myapp.ini',
}
$ini_settings = {
"cache_instance${i}_host" => {
section => 'cache_cluster',
setting => "${1}.host",
value => $cache_cluster_instance['host'],
},
"cache_instance${i}_weight" => {
section => 'cache_cluster',
setting => "${1}.weight",
value => $cache_cluster_instance['weight'],
}
}
create_resources('ini_setting', $ini_settings, $ini_settings_defaults)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment