【发布时间】:2023-03-27 09:36:01
【问题描述】:
我有一个执行检查并返回布尔值0|1 的 bash 脚本。
下面这样的脚本示例:
# 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 资源的触发器?
【问题讨论】: