Red Cricket
Red Cricket

Reputation: 10470

How can I avoid "write everything twice" in my hiera data?

Is there a better way to format my hiera data? I want to avoid the "write everything twice" problem.

Here is what I have now:

[root@puppet-el7-001 ~]# cat example.yaml 
---
controller_ips: 
 - 10.0.0.51
 - 10.0.0.52
 - 10.0.0.53
controller::horizon_cache_server_ip: 
 - 10.0.0.51:11211
 - 10.0.0.52:11211
 - 10.0.0.53:11211

I was wondering if there is functionality avaialble in hiera that is like Perl's map function. If so then I could do something like:

controller::horizon_cache_server_ip: "%{hiera_map( {"$_:11211"}, %{hiera('controller_ips')})}"

Thanks

Upvotes: 3

Views: 133

Answers (2)

Felix Frank
Felix Frank

Reputation: 8223

The mutation is a problem. It is simpler with identical data thanks to YAML's referencing capability.

controller_ips: &CONTROLLERS
 - 10.0.0.51
 - 10.0.0.52
 - 10.0.0.53
controller::horizon_cache_server_ip: *CONTROLLERS

You will need more logic so that the port can be stored independently.

controller::horizon_cache_server_port: 11211

The manifest needs to be structured in a way that allows you to combine the IPs with the port.

Upvotes: 1

kkamil
kkamil

Reputation: 2595

It depends on which puppet version you are using. I puppet 3.x, you can do the following:

common::test::var1: a
common::test::var2: b

common::test::variable:
 - "%{hiera('common::test::var1')}"
 - "%{hiera('common::test::var2')}"

common::test::variable2:
 - "%{hiera('common::test::var1')}:1"
 - "%{hiera('common::test::var2')}:2"

In puppet 4.0 you can try using a combination of zip, hash functions from stdlib, with built in function map. Something like:

$array3 = zip($array1, $array2)
$my_hash = hash($array3)
$my_hash.map |$key,$val|{ "${key}:${val}" }

Upvotes: 2

Related Questions