문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top