我有一个bash脚本,它执行检查并返回布尔0|1。这类脚本的示例如下:
# 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脚本,我需要使用以下代码:
$check_kernel_cmd="/path/to/script/check_kernel.sh"
exec {'check_kernel':
provider => shell,
returns => [ "0", "1", ],
command => "$check_kernel_cmd",
}因此,现在我需要使用上述exec资源Exec['check_kernel']的返回退出状态作为另一个exec资源Exec['reboot_node']的触发器,如下所示:
if $check_kernel == '1' {
$reboot = "/sbin/runuser - root -s /bin/bash -c '/sbin/shutdown -r'"
exec {'reboot_node':
provider => shell,
command => "$reboot",
}
}或者另一种方式可能是使用unless,如下所示:
$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资源的触发器?
发布于 2014-08-12 13:19:49
这是行不通的。将第一个脚本设置为外部事实,以便可以从清单中的变量查询其结果。或者,如果这是有效的,则通过后者的onlyif或unless参数调用前面的脚本,而不是作为自己的exec资源。
长答案
您心目中的方案与Puppet的主/代理范例不兼容。完整的清单是一次编译的,导致一个抽象的表示,目录。整个目录被发送给代理进行评估。只有到那时,代理才会启动和同步资源,包括两个exec资源。关于其中任何一个的返回值的信息不能在清单中使用,因为在这一点上清单不再可用。
主服务器使用来自代理计算机的信息的规范方法是自定义事实。在编译之前,将代码放在代理使用和运行的主服务器上。所有事实值都可以作为变量在清单中使用。
在像您这样的简单情况下,对检查脚本使用exec可能是不必要的。我相信下面的方法会有效的。
exec {
'/sbin/shutdown -r':
unless => '/path/to/script/check_kernel.sh';
}最后注意:编程您的木偶代理来重新启动您的节点是相当危险的--默认情况下,代理在启动时运行,因此如果逻辑中断,它可能会陷入恶性循环(您可能会在主服务器上修复这个问题,但它仍然不是一个令人愉快的视角)。
https://stackoverflow.com/questions/25263746
复制相似问题