Puppet 如何判断变量是否已设置
Puppet how to tell if a variable is set
在木偶中 class 我应该如何测试变量是否已设置?现在我只是检查一个变量是否未定义:
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,它不会 运行 命令。
如果你想测试变量是否已经定义,你应该这样做:
if defined('$http_port') {
$run_command = "$run_command --http-port $http_port"
}
参见 https://docs.puppet.com/puppet/4.10/function.html#defined。
如果 var 是一个 class 变量,你可以这样做:
class your_class (
Optional[Integer[0, 65535]] $http_port = undef,
) {
if $http_port {
notify { "got here with http_port=${http_port}": }
}
}
如果 class 声明时 http_port 设置为 0 到 65535 之间的整数,则它只会 运行 通知。
在木偶中 class 我应该如何测试变量是否已设置?现在我只是检查一个变量是否未定义:
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,它不会 运行 命令。
如果你想测试变量是否已经定义,你应该这样做:
if defined('$http_port') {
$run_command = "$run_command --http-port $http_port"
}
参见 https://docs.puppet.com/puppet/4.10/function.html#defined。
如果 var 是一个 class 变量,你可以这样做:
class your_class (
Optional[Integer[0, 65535]] $http_port = undef,
) {
if $http_port {
notify { "got here with http_port=${http_port}": }
}
}
如果 class 声明时 http_port 设置为 0 到 65535 之间的整数,则它只会 运行 通知。