Ask Your Question
0

Iterate optional hiera hash

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

cmseal84 gravatar image

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

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

mymodule::myclass::domains:
  some.domain.com:
    required: "this_required_string"
  anotherdomain.com:
    required: "another_required_string"
    optional: "this_string_is_optional"

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

mymodule::myclass(
  Optional[Hash] $domains = {},
){
   if $domains{
     //iterate domain keys and use the values to do things
     $domains.each{
       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

Comments

Are you creating a resource directly from the keys? Then you can use the 'splat' operator - https://puppet.com/docs/puppet/5.3/lang_resources_advanced.html#setting-attributes-from-a-hash

DarylW gravatar imageDarylW ( 2018-09-21 09:53:35 -0500 )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 -0500 )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 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

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

DarylW gravatar image

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

The answer to https://ask.puppet.com/question/35173... can probably help you out there - https://puppet.com/docs/puppet/5.5/la...

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

Comments

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 -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

1 follower

Stats

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

Seen: 91 times

Last updated: Sep 24