【问题标题】:Puppet: conditional restart of servicePuppet:有条件重启服务
【发布时间】:2020-06-05 18:07:04
【问题描述】:

是否可以有条件地跳过 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 资源。像我对 exec 资源所做的那样,将服务资源包装到 if 块中也不起作用,因为 service 资源可以做多种事情:

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

我只想有条件地阻止 (3) 的发生,同时始终执行 (1) 和 (2)。有没有办法做到这一点?

【问题讨论】:

    标签: puppet


    【解决方案1】:

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

    类似这样的:

    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,
    }
    

    【讨论】:

      【解决方案2】:

      tectux 的想法非常好。我已经增强了 if 条件。我决定为自定义事实使用脚本。

      模块/autofs/facts.d/autofs.sh

      #!/bin/sh
      DECISION=`test -f /usr/bin/docker && /usr/bin/docker ps -q |grep -q . && echo no`
      
      if [ "x$DECISION" == "x" ] ; then
          DECISION=yes
      fi
      echo '{'
      echo '  "autofs": {'
      echo "     \"do_automatic_restart\": \"$DECISION\""
      echo '  }'
      echo '}'
      

      所以,脚本的输出

      # modules/autofs/facts.d/autofs.sh 
      {
        "autofs": {
           "do_automatic_restart": "yes"
        }
      }
      

      现在我们可以使用自定义事实

      if $facts['autofs']['do_automatic_restart'] == "no" {                                                                                                                                            
          $_attr = { 'restart' => "logger puppet agent: automounter is NOT going to be RESTARTED due to active containers on a host" }                                                                                                                                                      
      } else {                                                                                                                                                                                         
          $_attr = {}                                                                                                                                                                                  
      }                                                                                                                                                                                                
      

      【讨论】:

        猜你喜欢
        • 2019-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-15
        • 2023-04-09
        • 1970-01-01
        相关资源
        最近更新 更多