Ask Your Question

Is there a way to simplify profiles via hiera_hash?

asked 2015-12-02 14:53:34 -0600

catpants gravatar image

updated 2015-12-02 15:02:14 -0600

Hi folks,

Using puppetlabs/apache from forge.

I'm trying to simplify vhost creation, and within my profiles::apache class I found myself doing this:

$access_log = hiera("profiles::apache::vhost::access_log")      
$error_log = hiera("profiles::apache::vhost::error_log")        
$docroot = hiera("profiles::apache::vhost::docroot")            
$docroot_owner = hiera("profiles::apache::vhost::docroot_owner")
$docroot_group = hiera("profiles::apache::vhost::docroot_group")
$port = hiera("profiles::apache::vhost::port")                  
$priority = hiera("profiles::apache::vhost::priority")          
$servername = hiera("profiles::apache::vhost::servername") 
$vhost_fname = hiera("profiles::apache::vhost::vhost_fname")

and then:

apache::vhost { $vhost_fname:          
  access_log => $access_log,           
  error_log => $error_log,             
  docroot => $docroot,                 
  docroot_owner => $docroot_owner,     
  docroot_group => $docroot_group,     
  port => $port,                       
  priority => $priority,               
  servername => $servername, 

It definitely seems like you should be able to simplifiy all this with a hiera_hash, but not sure what the code would look like. I know how to define a hash in hiera, and how to assign it to a variable, so maybe something like:

$all_params = hiera_hash("profiles::apache::vhost::all_params")

And then of course the variable names in my hiera file would have to match the names in the vhost block, ie "port" etc.

But then how do I pass those to the apache::vhost block? I would imagine it would involve some sort of builtin that I don't know about, maybe something like:

apache::vhost { $vhost_fname:
  all_params => $all_params,

What does the syntax look like for what I'm trying to do here?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-12-03 07:00:25 -0600

JohnsonEarls gravatar image

with a slightly modified hash, you can use the create_resources( function:

$all_params = { $vhost_fname => hiera_hash("profiles::apache::vhost::all_params") }
create_resources(apache::vhost, $all_params)

If going this route, and if you need or might need multiple vhosts, you might just want to do something like:

$vhost_defaults = hiera_hash("profiles::apache::vhost::defaults")
$all_vhosts = hiera_hash("profiles::apache::vhost::all_vhosts")
create_resources(apache::vhost, $all_vhosts, $vhost_defaults)

with a corresponding Hiera datasource like...

    access_log: (default access_log setting)
    error_log: (default error_log setting)
    docroot: (default docroot setting)
    docroot_owner: (default docroot_owner setting)
    docroot_group: (default docroot_group setting)
    port: (default port setting)
    priority: (default priority setting)

        servername: (servername for this vhost)
        docroot: (docroot, if different than the default)
        docroot_owner: (docroot owner, if different than the default)
        servername: (servername for this vhost)
edit flag offensive delete link more


Works great! Thank you!

catpants gravatar imagecatpants ( 2015-12-03 13:04:13 -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: 2015-12-02 14:53:34 -0600

Seen: 7,398 times

Last updated: Dec 03 '15