Domanda

There is a language pattern I would like to use in puppet and am having trouble figuring out how to implement it. Let's say I have a parameterized class:

class ntp (
  $ensure             = 'present',
  $ntp_package_ensure = 'present',
  $ntp_package_name   = 'ntp',
  $ntp_config_ensure  = 'file',
  $ntp_config_path    = '/etc/ntp.conf',
  $ntp_service_ensure = 'running',
  $ntp_service_enable = true,
  $ntp_service_name   = 'ntpd',
) {

I would then really like to be able to say:

  if $ensure == 'absent' {
    $ntp_package_ensure = 'absent',
    $ntp_config_ensure  = 'absent',
    $ntp_service_ensure = 'stopped',
  }

But because puppet variables (constants?) are immutable I'm getting syntax errors. So What is puppets way of doing this? The only solution I can see right now is to add a monolith if statement that partitions the code into an ensure section and an else section.

È stato utile?

Soluzione

The most straight forward workaround is to actually introduce new local variables.

case $ensure {
    'absent': {
        $my_ntp_package_ensure = 'absent'
        $my_ntp_config_ensure  = 'absent'
        $my_ntp_service_ensure = 'stopped'
    }
    'present': {
        ...
    }
    default: {
        $my_ntp_package_ensure = $ntp_package_ensure
        $my_ntp_config_ensure  = $ntp_config_ensure
        $my_ntp_service_ensure = $ntp_service_ensure
    }
}

Then only use the local counterparts instead of the parameters proper.

I recommend to refrain from such an API design and instead ask a new question concerning the actual scenario and how to structure the ntp module better.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top