Ask Your Question

How can I collect values from Hiera in a hash.

asked 2015-07-28 07:42:25 -0500

Krist van Besien gravatar image

I have wrapper class that I want to use to set up a nginx server, as well as set up firewall ports and selinux ruls.

So I have for example this in my hiera data:

    listen_port:    8081
    www_root:       /usr/share/nginx/html
    listen_port:    8082
    www_root:       /usr/share/nginx/html

I use this in createsources, with the defined type that comes with the nginx module I use, and this works as expected.

However I also need to create firewall rules. So I add something like this:

 iptables::rule { $ports: }

This is done using a custom defined type. In order to tell it which ports need to be opened I have this in hiera:

  - 8081
  - 8082

As you can see this means that whenever I add a virtual host to hiera I need to specify the port twice. Is there a way around this? Is there a way to just collect all the "listen_port" values in an array, that I can use to create the iptables::port resources?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2015-07-28 08:50:29 -0500

updated 2016-07-29 04:47:36 -0500


I'm looking to do something similar soon.

I think in Puppet 4 it is possible to use iteration...

something like !untested!:

$vhosts = hiera_hash('my_nginx::vhosts')

$vhosts.each |$key| { 
  # you should then be able to access listen_port within each key ---
  listen_port = $key[listen_port]
  # create your iptables::rule resource here
  iptables::rule { $listen_port: }

the following link may give you some more info:

As for Puppet <4 I think the only way would be to create your own defined type, which creates an iptables::rule resource and an nginx::vhost resource, then use a similar create_resources call but to your defined type.

HTH, Josh

edit flag offensive delete link more

answered 2016-07-29 08:08:15 -0500

DarylW gravatar image

Two possible answers for puppet 3, if that's where you're doing it.

1) Pass your mynginx::vhost structure into a custom function, and do the manipulations you need in ruby to turn it into the array/hash of values you need, and then pass that to a createresources call.

2) If you only need that single array, there may be some way to collect out just the values of that key, sort and unique them.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2015-07-28 07:42:25 -0500

Seen: 345 times

Last updated: Jul 29 '16