1

We're using the roles/profiles pattern. In the example below I'd like to optionally pass a parameter from hiera so that if no value is provided then the ::apache class default is used (which it gets internally from its ::apache::params class). This would enable us to set the keepalive value on some servers but allow the ::apache class to determine the value itself when we don't.

common.yaml
-----------
profiles::apache::keepalive               : On
profiles::apache::keepalive_timeout       : 5


apache.pp
---------
class profiles::apache {

  $apache_keepalive              = hiera('profiles::apache::keepalive')
  $apache_keepalive_timeout      = hiera('profiles::apache::keepalive_timeout')

  class {'::apache':
    keepalive              => $apache_keepalive,
    keepalive_timeout      => $apache_keepalive_timeout,
  }

}

I've tried setting the default value of the hiera lookup to 'undef' but then I end up with empty values in the config.

030
  • 5,901
  • 13
  • 68
  • 110
Michelle
  • 923
  • 5
  • 20
  • 30

2 Answers2

1

You achieve this effect through automatic parameter lookup.

Your Hiera data targets the actual apache module, not your apache profile.

apache::keepalive: true

This only works if class apache has a $keepalive parameter.

And yes, the default for this should be taken from apache::params.

class apache($keepalive = $::apache::params::keepalive)
    inherits ::apache::params { 
        ...
}
Felix Frank
  • 3,093
  • 1
  • 16
  • 22
  • The roles/profiles pattern doesn't use automatic parameter lookup. Profiles are wrapper classes that abstract component modules and its suggested that doing the hiera lookups explicitly within the profile is a best practice (so you can quickly tell what has and hasn't been set). – Michelle May 19 '15 at 16:06
  • Hmm, listening to [Gary](http://garylarizza.com/blog/2014/02/17/puppet-workflow-part-2/) is usually a good idea and he does raise some valid points. It's not necessarily Best Practice as seen by the community at large, though. In the end, you be the judge. The Roles/Profiles pattern is agnostic on this question, though. Note that it works quite well with automatic lookup, because profiles can still overrule Hiera through the resource like class declaration syntax. – Felix Frank May 19 '15 at 20:37
  • Thanks Felix. We will clearly have to use automatic parameter lookup in this case. – Michelle May 20 '15 at 07:37
0

I personally prefer to have all my variables explicitly declared. In this case I would declare the default value on common.yaml and then use other yaml files to declare values that differ from the default like

common.yaml
-----------
profiles::apache::keepalive               : On
profiles::apache::keepalive_timeout       : 5

servertype.yaml
---------------
profiles::apache::keepalive               : On
profiles::apache::keepalive_timeout       : 30

Then set the hierarchy on hiera.yaml

:hierarchy: - servertype.yaml
            - common.yaml

If you don't want to do this, then I think the only other approach would be to use if statements

apache.pp
---------
class profiles::apache {

  $apache_keepalive              = hiera('profiles::apache::keepalive')
  $apache_keepalive_timeout      = hiera('profiles::apache::keepalive_timeout')

  if ($apache_keepalive == undef) {
    class {'::apache': }
  }
  else {
    class {'::apache':
      keepalive              => $apache_keepalive,
      keepalive_timeout      => $apache_keepalive_timeout,
    }
  }

}
Luis
  • 306
  • 1
  • 2
  • 11