在木偶类中,我应该如何测试变量是否已设置?现在我只是检查变量是否未定义:
if $http_port != undef {
$run_command = "$run_command --http-port $http_port"
}
有没有更好的方法来检查变量是否已声明?
如果您正在测试变量是否为 undef,那么您的方法是正确的。写作
if $http_port {
$run_command = "$run_command --http-port $http_port"
}
几乎可以完成相同的任务。如果 $http_port 为 undef 或 false,则不会运行该命令。
如果你想测试 var 是否已定义,你应该这样做:
if defined('$http_port') {
$run_command = "$run_command --http-port $http_port"
}
See https://docs.puppet.com/puppet/4.10/function.html#define https://docs.puppet.com/puppet/4.10/function.html#defined.
如果 var 是一个类变量,你可以这样做:
class your_class (
Optional[Integer[0, 65535]] $http_port = undef,
) {
if $http_port {
notify { "got here with http_port=${http_port}": }
}
}
然后,仅当声明类时将 http_port 设置为 0 到 65535 之间的整数时,它才会运行通知。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)