Ask Your Question
0

pass params to inherits resource

asked 2015-11-26 18:37:36 -0500

dpalumbo gravatar image

Hi,

how can i pass a parameter to the resource to be inherted? i need to make decision in params based on a parameter, $ldap_enable, that can be passed not only by hiera.

of course, also alternative solutions are welcome :)

example code:

class myclass(
  $package               = $myclass::params::source,
  $ldap_enable         = false,
) inherits myclass::params {

...
}

class myclass::params ( $ldap_enable ) {

  if ( $::ldap_enable == true ) {
    $package = 'packageldap'
   } else {
    $package = 'packagestandard'
   }
...
}
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2015-11-30 02:44:00 -0500

domcleal gravatar image

This isn't possible in the way you're trying to write it. I'd suggest setting the default value of $package to undef, then in the body of myclass, set it based on $ldap_enable unless already set.

class myclass(
  $package             = undef,
  $ldap_enable         = false,
) inherits myclass::params {
  unless $package {
    $package = $ldap_enable ? {
      true    => 'packageldap',
      default => 'packagestandard',
    }
  }
}
edit flag offensive delete link more
0

answered 2015-11-29 08:44:23 -0500

You would want to switch this around:

class myclass (
  $ldap_enable = $::myclass::params::ldap_enable
) inherits ::myclass::params {
  if $ldap_enable {
    $package = 'packageldap'
  } else {
    $package = 'packagestandard'
  }
}

class myclass::params {
  $ldap_enable = false
}

This would allow you to set ldap_enable at instantiation or via hiera or use the default of false. Unfortunately, the package evaluation is in the main class at this point. The only other way to go to keep it in params, would be to use one more level below params:

class myclass::params inherits ::myclass::globals {
  $ldap_enable = pick($::myclass::globals::ldap_enable, false)

  if $ldap_enable {
    $package = 'packageldap'
  } else {
    $package = 'packagestandard'
  }
}

class myclass::globals (
  $ldap_enable = undef
) {
  # Stub for more globals
}

Then you could just call package { $package: } in your main class that inherits params like I have listed above and remove the logic. If you wanted to override, you'd have to override myclass::globals::ldap_enable in hiera or via class instantiation like this:

class { '::myclass::globals':
  ldap_enable => true,
} ->
class { 'myclass': }

I think the globals scenario is far too complex for a single entry like this. If you had much more to derive logically BEFORE the params class point, then this would be the way to go.

Hopefully this helps.

edit flag offensive delete link more

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: 2015-11-26 18:37:36 -0500

Seen: 669 times

Last updated: Nov 30 '15