What is hiera?
Hiera is simple Lightweight Pluggable Hierarchical Database. It is a key/value lookup tool for hiera_lookupiguration data. Key/value may be string, array or hash.
Using Hiera
Suppose you have a a_hiera_hash in your yaml. It has a_hiera_key in two different yaml. At one place it is string ( hiera/location/dc1.yaml ) and at another place it is array (hiera/location/dc2.yaml)
==> hiera/location/dc1.yaml
a_hiera_hash:
a_hiera_key: item_string
==> hiera/location/dc2.yaml
a_hiera_hash:
a_hiera_key:
- item_one
- item_two
- item_three
Adding a string in a string or array returned by hiera_hash
Now, if you want to add another string 'item_string_2' at the end of a_hiera_hash, there are 2 cases:
Case-1:
system1.dc1.com - a system in DC1 - For this system, a_hiera_key will be evaluated as string.hiera_lookup = hiera_hash('a_hiera_hash', {})
hiera_lookup['a_hiera_key'] = 'item_string' << this is what we will get
So we need to add 'item_string_2' at the end of above string. Puppet code will look like below.
$a_hiera_key = $hiera_lookup['a_hiera_key']
$a_hiera_key_new = "${a_hiera_key} item_string_2"
$a_hiera_key = $a_hiera_key_new
Case-1:
system2.dc2.com - a system in DC2 - For this system, a_hiera_key will be evaluated as array.hiera_lookup = hiera_hash('a_hiera_hash', {})
a_hiera_hash['a_hiera_key'] = [ 'item_one', 'item_two', 'item_three' ] << this is what we will get
So, now we need to add 'item_string_2' string as a new item of above array ! (type mismatch)
There is no straight way to add string as a new item of array item !
1- Convert array item into space seprated string using join library function - 'item_one item_two item_three'
$a_hiera_key = join($hiera_lookup['a_hiera_key'], ' ')
2- Add 'item_string_2' at the end of string - 'item_one item_two item_three item_string_2'
$a_hiera_key_new = "${a_hiera_key} item_string_2"
3- Split string using space a separator to make an array using split function- [ 'item_one', 'item_two', 'item_three', 'item_string_2' ]
$a_hiera_key = split($a_hiera_key_new, ' ')
So, puppet code will look like something this.
hiera_lookup = hiera_hash('a_hiera_hash', {})
if is_array($hiera_lookup['a_hiera_key']) {
$a_hiera_key = join($hiera_lookup['a_hiera_key'], ' ')
$a_hiera_key_new = "${a_hiera_key} item_string_2"
$a_hiera_key = split($a_hiera_key_new, ' ')
} else {
$a_hiera_key = $hiera_lookup['a_hiera_key']
$a_hiera_key_new = "${a_hiera_key} item_string_2"
$a_hiera_key = $a_hiera_key_new
}
NOTE:
If dc1.yaml also has array (similar to dc2.yaml), hiera_has will automatically merge 2 arrays. For eacmple, if dc1.yaml array is following
a_hiera_hash:
a_hiera_key:
- item_dc1
Then, a_hiera_key will be merge as below.
hiera_lookup = hiera_hash('a_hiera_hash', {})
a_hiera_hash['a_hiera_key'] = [ 'item_one', 'item_two', 'item_three', 'item_dc1'] << this is what we will get
References:
https://github.com/puppetlabs/hiera (hiera)
https://docs.puppetlabs.com/hiera/1/lookup_types.html (hiera_hash)
https://docs.puppetlabs.com/references/latest/function.html (split)
https://forge.puppetlabs.com/puppetlabs/stdlib/readme (join)
No comments:
Post a Comment