【问题标题】:Puppet Exec tag onlyif troublesPuppet Exec 标记 onlyif 麻烦
【发布时间】:2016-12-16 21:01:46
【问题描述】:

我有这段代码:

if ($galera_master == $::fqdn) {
    if ( $vendor_type == 'GoofyIT' ) {
        $onlyif = [
            "ps -ef | grep mysqld_safe | grep wsrep-new-cluster > /dev/null",
            "test $desired_cluster_size == $(mysql --defaults-file=/root/.my.cnf -e \"SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size'\" | grep wsrep_cluster_size | awk '{print \$2}') > /dev/null",
        ]
        $unless = [ "test $desired_cluster_size -lt $(/usr/bin/mysql --defaults-file=/root/.my.cnf -e \"SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size'\" | grep wsrep_cluster_size | awk '{print \$2}')",
                    "ps -ef | grep mysqld_safe | grep wsrep-new-cluster && false",
        ]
        Exec['bootstrap_galera_cluster'] -> Exec['finish_bootstrap']
        exec { 'finish_bootstrap':
            path     => '/usr/bin:/bin:/usr/sbin:/sbin',
            command  => 'pkill -SIGQUIT mysqld; sleep 5; systemctl restart mysqld',
            onlyif   =>  $onlyif,
            #unless   =>  $unless,
        }
    }
}

在运行这段代码的节点上,我有以下条件:

# test 3 -eq $(/usr/bin/mysql --defaults-file=/root/.my.cnf -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size'" | grep wsrep_cluster_size | awk '{print $2}')
# echo $?
0
# ps -ef | grep mysqld_safe | grep wsrep-new-cluster 
# echo $?
1

所以我假设当 puppet 代理在我的节点上运行时,我不应该看到 pkill -SIGQUIT mysqld; sleep 5; systemctl restart mysqld 执行。然而,当我运行puppet agent -td 时,我看到傀儡代理正在执行pkill -SIGQUIT mysqld; sleep 5; systemctl restart mysqld

The documentation for Exec's onlyif tag 这么说:

仅当

如果设置了这个参数,那么这个 exec 只会在命令的退出代码为 0 时运行。例如:

exec { 'logrotate':
  path   => '/usr/bin:/usr/sbin:/bin',
  onlyif => 'test `du /var/log/messages | cut -f1` -gt 100000',
}

只有当测试返回 true 时才会运行 logrotate。

请注意,此命令遵循与主命令相同的规则,例如它作为哪个用户和组运行。这也意味着如果没有设置路径,它必须是完全限定的。

它还使用与主命令相同的提供程序,因此任何不同提供程序的行为都将匹配。

还要注意 onlyif 可以取一个数组作为它的值,例如:

onlyif => ['test -f /tmp/file1', 'test -f /tmp/file2'],

这只会在数组中的所有条件都返回 true 时运行 exec。

【问题讨论】:

  • 您的onlyifunless“命令”实际上是shell 管道。如果这是您需要的,那么您至少应该指定provider => 'shell',这不是默认值。
  • 这是一个很好的提示!但我发现我需要这样做:ps -ef | grep -v grep | grep mysqld_safe | grep wsrep-new-cluster 而不是 ps -ef | grep mysqld_safe | grep wsrep-new-cluster
  • 听起来您已经整理好了。好和好。如果我确信 provider 设置是一个完整的解决方案,那么我就会将其作为答案。
  • 如何确定我的默认提供者是什么?顺便说一句,感谢您的所有帮助! :)
  • 您可以通过查看the provider list for that type in the type reference 来确定默认提供程序。它将是“posix”或“windows”,因为“shell”不是任何人的默认值。从上下文来看,在你的情况下,我猜测“posix”似乎很安全。

标签: bash puppet


【解决方案1】:

事实证明我需要这样做:

ps -ef | grep -v grep | grep mysqld_safe | grep wsrep-new-cluster

而不是

ps -ef | grep mysqld_safe | grep wsrep-new-cluster

【讨论】:

  • 另外,我喜欢做类似ps -ef | grep [m]ysqld_safe | grep [w]srep-new-cluster 的事情,因为它对我来说稍微干净一些。还可以考虑使用-q 作为> /dev/null 的更清洁替代品。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
  • 2011-06-12
相关资源
最近更新 更多