Ask Your Question

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

asked 2015-05-06 13:46:55 -0500

red-cricket gravatar image

updated 2015-05-06 13:51:59 -0500

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 

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')})}"

Also it would be nice if there were a hiera_join so I could do:

   memcached_servers: "%{hiera_join( ",", ${hiera('controller::horizon_cache_server_ip')})}"


edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2015-05-06 17:49:41 -0500

JohnsonEarls gravatar image

updated 2015-05-06 17:51:57 -0500

If the values of those keys will always be linked like that, then do the "write it twice" logic in your code rather than in Hiera:

controller::horizon_cache_port: 11211

and in code...

$controller_ips = hiera_array('controller_ips')
$horizon_cache_port = hiera('controller::horizon_cache_port')
$horizon_cache_server_ip = regsubst($controller_ips, '$', ":${horizon_cache_port}")
# 'join' function requires puppetlabs-stdlib module
$memcached_servers = join($horizon_cache_server_ip, ",")

whoops, I just saw the "I'm not supposed to modify the module" comment below. What if, instead of modifying that module, you write a wrapper module that does the above conjugations, then calls the pristine module with the correct parameters?

edit flag offensive delete link more

answered 2015-05-06 13:52:18 -0500

arussell gravatar image

I think you could use something like this.

controller::horizon_cache_server_ip: "%{hiera('controller_ips')}:11211"


edit flag offensive delete link more


That's gives me `["", "", ""]:11211` when I run `# hiera -c ./hiera.yaml controller::horizon_cache_server_ip`. Not quite what I was looking for.

red-cricket gravatar imagered-cricket ( 2015-05-06 14:12:20 -0500 )edit

You could also change the module to lookup the data differently. Change this controller::horizon_cache_server_ip: to just the port. Then in the module {hiera('controller_ips'):{hiera('cache_server_port')} Just a thought.

arussell gravatar imagearussell ( 2015-05-06 15:16:40 -0500 )edit

Thanks for your answer and comments. I am not suppose to modify the module code, so I guess I am stuck with just having to repeat the list of ip addresses.

red-cricket gravatar imagered-cricket ( 2015-05-06 15:49:37 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2015-05-06 13:46:55 -0500

Seen: 124 times

Last updated: May 06 '15