首页 > 解决方案 > Puppet:有条件重启服务

问题描述

是否可以有条件地跳过service资源上的刷新事件?或者:service当类被通知时,是否可以防止类内的资源被刷新?


上下文:我有一个 Puppet 模块,其中包含以下清单(简化):

class foo(
  Boolean pre_process_service  = true,
  Boolean auto_restart_service = true
) {
  if $pre_process_service {
    exec { 'process config':
      ... # details including a pretty complex command - should be hidden in my module
      notify => Service['foo'],
    }
  }

  service { 'foo':
    ensure => 'running',
    enable => true,
  }
}

可以这样使用:

file { 'config':
  ... # copies config from somewhere
}

class { 'foo':
  auto_restart_service => false,
  subscribe            => File['config'],
}

当用户指定时如何避免重新启动服务auto_restart_service => false

请注意,模块的用户决定如何提供配置(复制文件,签出 Git 存储库,......)所以我不能在我的模块中这样做。相反,该类订阅提供配置的资源。只要用户使用默认设置,auto_restart_service = true一切都可以正常工作,甚至禁用配置的预处理也可以正常工作。但是,当用户指定时auto_restart_service = false,服务仍然会重新启动,因为在service通知类时会刷新资源。if像我对资源所做的那样将服务资源包装到一个块exec中也不起作用,因为service资源做了多种事情:

  1. 如果服务没有运行,它会启动服务
  2. 如果未启用,它将启用服务
  3. 如果收到通知,它将重新启动服务

我只想有条件地防止(3)发生,同时总是做(1)和(2)。有没有办法做到这一点?

标签: puppet

解决方案


我认为当您通知班级时没有办法不刷新服务。但是,您可以尝试使用资源的restart属性有条件地覆盖 Puppet 应该如何重新启动服务service

像这样的东西:

if $auto_restart_service {

  # Let the provider handle the restart
  $_attr = {}

} else {

  # Let Puppet execute `true` instead of actually restarting the service
  $_attr = { 'restart' => '/usr/bin/true' }

}

service { 'foo':
  ensure => 'running',
  enable => true,
  *      => $_attr,
}

推荐阅读