ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 检测字符串中是否包含指定的值 想知道什么在什么不在吗?Puppet 的 in 可以帮你, 如下面的表达式: ``` if "foo" in $bar ``` 如果 foo 是 $bar 的子串,表达式的值为 true。 如果 $bar 是个数组,并且 foo 是这个数组中的一个元素,表达式的值为 true。 如果 $bar 是一个哈希,foo 是 $bar 的一个键值,表达式的值为 true。 #### 操作步骤 1. 在你的配置清单中添加如下代码: ``` if $operatingsystem in [ "Ubuntu", "Debian" ] { notify { "Debian-type operating system detected": } } elsif $operatingsystem in [ "RedHat", "Fedora", "SuSE", "CentOS" ] { notify { "RedHat-type operating system detected": } } else { notify { "Some other operating system detected": } } ``` 2. 运行 Puppet: ``` # puppet agent --test Debian-type operating system detected ``` #### 更多用法 in 表达式既可以使用在 if 语句或其他条件语句中,还可以在任何表达式能出现的地方使用。 例如,你可以像下面这样为一个变量赋值: ``` $debianlike = $operatingsystem in [ "Debian", "Ubuntu" ] if $debianlike { $ntpservice = "ntp" } else { $ntpservice = "ntpd" } ```