# How can I collect values from Hiera in a hash.

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:

my_nginx::vhosts:
localhost_8081:
listen_port:    8081
www_root:       /usr/share/nginx/html
localhost_8082:
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: my_nginx::ports: - 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 close merge delete ## 2 Answers Sort by » oldest newest most voted Hi, 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: }
}


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

more

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.

more