Puppet - 使用自定义事实的 IF 语句不起作用

问题描述 投票:0回答:2

if 语句无法使用自定义事实,我尝试一下:

notify { "FACTER value= ${facts['versionrepokubelocal']}": }
if $facts['versionrepokubelocal'] !=  '2' {
  notify { "Upgrading repository version": }
}

我在“if”之前使用notify来检查自定义事实的值。 尝试使用 puppet apply (与远程服务器上的 puppet agent -t 相同)得到以下输出:

Notice: FACTER versionrepokubelocal value = 2
Notice: /Stage[main]/Kube::Config/Notify[FACTER versionrepokubelocal value = 2]/message: defined    'message' as 'FACTER versionrepokubelocal value = 2'
Debug: /Stage[main]/Kube::Config/Notify[FACTER versionrepokubelocal value = 2]: The container   Class[Kube::Config] will propagate my refresh event
Notice: Upgrading repository version
Notice: /Stage[main]/Kube::Config/Notify[Upgrading repository version]/message: defined 'message' as  'Upgrading repository version'

在执行之前和之后,我检查了服务器上自定义事实的值,它的值正确,如上所示:

[root@myserver 清单]$facter versionrepokubelocal 2

我不明白为什么它不能正常工作,如果事实被正确评估并且语句的语法正确(使用 pdk 验证和 pdk 测试单元测试),它不应该打印“升级存储库版本”。

if-statement puppet facter
2个回答
1
投票

该因素的值看起来像数字,因此如果它是数字,请更改您的 if 条件,不带引号的值如下

if $facts['versionrepokubelocal'] !=  2 {   
  notify { "Upgrading repository version": } 
}

0
投票

谢谢维克多! 我的最终目标是在 if 语句条件中使用 hiera 变量($releasepkg),我尝试如下但不起作用:

$releasepkg = lookup ({ 'name' => 'kube::install::releasepkg','default_value' => undef })

if $facts['versionrepokubelocal'] !=  $releasepkg {
  notify { "Upgrading repository version...": }
  notify { " versionrepokubelocal = ${facts['versionrepokubelocal']}":
  notify { " releasepkg = ${releasepkg}": }
}


$releasepkg defined in yaml file as below :

---
kube::install::releasepkg: '1_28'

puppet 代理运行的输出:

Notice: Upgrading repository version...
Notice: /Stage[main]/Kube::Config/Notify[Upgrading repository 
version...]/message: defined 'message' as 'Upgrading repository 
version...'
Notice:  versionrepokubelocal = 1.28
Notice: /Stage[main]/Kube::Config/Notify[ versionrepokubelocal = 
1.28]/message: defined 'message' as ' versionrepokubelocal = 1.28'
Notice:  releasepkg = 1.28
Notice: /Stage[main]/Kube::Config/Notify[ releasepkg = 1.28]/message: 
defined 'message' as ' releasepkg = 1.28'
Notice: Applied catalog in 7.68 seconds

有什么想法吗? 非常感谢

© www.soinside.com 2019 - 2024. All rights reserved.