Ask Your Question

Iterate optional hiera hash

asked 2018-09-20 10:26:24 -0600

cmseal84 gravatar image

updated 2018-09-24 10:12:33 -0600

I've got the following hiera, but it's optional...

    required: "this_required_string"
    required: "another_required_string"
    optional: "this_string_is_optional"

This is my skeleton for the class I want to use it in:

  Optional[Hash] $domains = {},
   if $domains{
     //iterate domain keys and use the values to do things
       apache::vhost{ $domains.key:
              servername  => $domains.key,
              serveralias => $domains.optionalvalue,
              docroot     => $domains.requiredvalue
       ->letsencrypt::certonly{ $domains.key:
          webroot_paths => $domains.requiredvalue
          domains       => [$domains.key, $domains.optionalvalue],

I'm just not sure how to iterate through each domain key, and access the required value, and optional value (if there is one). The above is just to give you an idea.

edit retag flag offensive close merge delete


Are you creating a resource directly from the keys? Then you can use the 'splat' operator -

DarylW gravatar imageDarylW ( 2018-09-21 09:53:35 -0600 )edit

I like that, but unfortunately I'm using the domain key, required and optional values to create a few resources inside the 'if' statement. I figure I'm going to need some kind of 'for' loop or '.each' to iterate the domain keys, then use required and optional values.

cmseal84 gravatar imagecmseal84 ( 2018-09-24 03:53:26 -0600 )edit

so, do you always want the letsencrypt block, but only add the optionalValue to the domains if it exists?

DarylW gravatar imageDarylW ( 2018-09-25 08:26:44 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2018-09-24 09:02:45 -0600

DarylW gravatar image

updated 2018-09-24 09:05:10 -0600

The answer to can probably help you out there -

Here is the example from the docs

Declaring resources The focus of the Puppet language is declaring resources, so most people want to use iteration to declare many similar resources at once. In this example, there is an array of command names to be used in each symlink’s path and target. The each function makes this succinct.

$binaries = ['facter', 'hiera', 'mco', 'puppet', 'puppetserver']

$binaries.each |String $binary| {
  file {"/usr/bin/${binary}":
    ensure => link,
    target => "/opt/puppetlabs/bin/${binary}",

That shows you how to iterate over the array (with .each), and inside of the block you can make all of the resources you need to, and conditionally check if the optional property exists or not.

If you provide a more concrete example of what you want to do with the values, I can give a more tailored answer.

edit flag offensive delete link more


I've updated my original example to give some more detail as to how I'll be using the hiera hash - thanks again for getting back to me and offering to help! :)

cmseal84 gravatar imagecmseal84 ( 2018-09-24 10:13:25 -0600 )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

1 follower


Asked: 2018-09-20 10:26:24 -0600

Seen: 115 times

Last updated: Sep 24