木偶,如何使用exec返回值作为触发器? [英] Puppet, how to use the exec return value as a trigger?

查看:147
本文介绍了木偶,如何使用exec返回值作为触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本,该脚本执行检查并返回布尔值0|1. 下面的脚本示例:

I have a bash script that perform a check and return a boolean 0|1. Example of such script below :

# less /path/to/script/check_kernel.sh
#! /bin/bash
# Check if running kernel match default=0 kernel in grub.conf
KERNEL_RUNN=`/bin/uname -r | /bin/sed -e 's/^//' -e 's/[[:space:]]*$//'`
KERNEL_GRUB=`/bin/grep kernel /boot/grub/menu.lst | /bin/grep -v '#' \
| /bin/awk '{print $2}' | /bin/sed -e 's/\/vmlinuz-//g' | /usr/bin/head -1 \
| /bin/sed -e 's/^//' -e 's/[[:space:]]*$//'`

if [ "$KERNEL_RUNN" == "$KERNEL_GRUB" ]; then
  exit 0
else
  exit 1
fi

要在Puppet中运行上述shell脚本,我将使用以下代码:

To run the above shell script in Puppet I would use the following code :

$check_kernel_cmd="/path/to/script/check_kernel.sh"
exec {'check_kernel':
  provider => shell,
  returns => [ "0", "1", ],
  command => "$check_kernel_cmd",
}

所以现在我需要使用以上执行资源Exec['check_kernel']的返回退出状态作为另一个执行资源Exec['reboot_node']的触发器,例如:

So now I need to use the returned exit status of above exec resource Exec['check_kernel'] as a trigger to another exec resource Exec['reboot_node'], something like :

if $check_kernel == '1' {
  $reboot = "/sbin/runuser - root -s /bin/bash -c '/sbin/shutdown -r'"
  exec {'reboot_node':
    provider => shell,
    command => "$reboot",
  }
}

或另一种样式方法可能是按如下方式使用unless:

or maybe another style approach would be to use unless as follows :

$reboot = "/sbin/runuser - root -s /bin/bash -c '/sbin/shutdown -r'"
exec {'reboot_node':
  provider => shell,
  command => "$reboot",
  unless => "/bin/echo $check_kernel",
  require => Exec['check_kernel'],
}

使用exec资源的退出状态作为同一清单中另一个exec资源的触发器的推荐方法/代码是什么?

What would the recommended approach/code be to use the exit status of an exec resource as a trigger to another exec resource in the same manifest ?

推荐答案

TL; DR,此方法无效.将您的第一个脚本设为外部事实,以便您查询其脚本来自清单中变量的结果.或者,如果那是有效的,则通过前一个脚本的onlyifunless参数而不是作为其自己的exec资源来调用前一个脚本.

TL;DR this cannot work. Make your first script an external fact so that you can query its result from a variable in your manifests. Alternatively, if that is valid, call the prior script through the latter's onlyif or unless parameter, instead of as its own exec resource.

好答案

您想到的方案与Puppet的master/agent范式不兼容.一次编译完整的清单,生成目录的抽象表示.整个目录将发送给代理进行评估.只有这样,代理才会启动和同步资源,包括两个exec资源.有关清单中任何一个的返回值的信息都不能在清单中使用,因为清单此时已不可用.

The scheme you have in mind is not compatible with Puppet's master/agent paradigm. The complete manifest is compiled in one go, leading to an abstract representation, the catalog. This whole catalog is sent to the agent for evaluation. Only then will the agent start and sync resources, including both exec resources. The information about the return value of any of them cannot be used in the manifest, because the manifest is no longer available at that point.

主服务器使用代理计算机中信息的规范方式是自定义事实".您将代码放置在代理要消耗的主数据库上并在编译之前运行它.所有事实值都可以在清单中用作变量.

The canonical way for the master to use information from the agent machine are Custom Facts. You place code on the master which the agent consumes and runs prior to compilation. All fact values can be used in the manifest as variables.

在像您这样的简单情况下,可能不需要使用exec作为检查脚本.我相信以下方法会起作用.

In a simple case such as your's, using an exec for the check script is likely unnecessary. I believe the following would work.

exec {
    '/sbin/shutdown -r':
        unless => '/path/to/script/check_kernel.sh';
}

最后的提示:对Puppet代理进行编程以通过某些本地逻辑重新启动节点可能会非常危险-代理默认在启动时运行,因此可能会终止如果这种逻辑中断了,您将陷入一个恶性循环(您可能可以将其修复在主数据库上,但这仍然不是乐观的看法).

Final note: Programming your Puppet agent to reboot your nodes by some homegrown logic could be rather dangerous - the agent runs at startup by default, so it may end up in a vicious cycle if that logic breaks (you can likely fix that on the master, but it's still not a cheerful perspective).

这篇关于木偶,如何使用exec返回值作为触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆