傀儡逻辑条件-“onlyif”和“unless”不起作用

傀儡逻辑条件-“onlyif”和“unless”不起作用

我已经编写了以下代码,如果没有交换,它可以正常工作,但是一旦创建了交换并且再次执行脚本,就会导致错误。

我尝试使用 onlyif 和 except 添加条件,它们在下面的代码中被注释掉,但似乎都不起作用

class swap {

   exec { "create swap file":
      command => "dd if=/dev/zero of=/swapfile bs=1M count=1024",
      path     => "/bin/",
      creates => "/swapfile",
   }

   exec { "makeswap":
      command => "mkswap /swapfile",
      path     => "/sbin/",
      # condition so that the block is executed only if the swap is not place
      onlyif => "/sbin/swapon -s | /bin/grep file > /dev/null",
      require => Exec['create swap file'],
   }

   exec { "enable swap":
      command => "swapon /swapfile",
      path     => "/sbin/",
      require => Exec['makeswap'],
   }
}
include swap

下面是调试时的控制台日志

[root@puppet testpuppet]# puppet apply swap.pp --debug
...
Debug: Finishing transaction 70032061368300
Debug: Loaded state in 0.00 seconds
Debug: Loaded state in 0.00 seconds
Debug: /Stage[main]/Swap/Exec[makeswap]/require: requires Exec[create swap file]
Debug: /Stage[main]/Swap/Exec[enable swap]/require: requires Exec[makeswap]
Info: Applying configuration version '1404202521'
Debug: Exec[makeswap](provider=posix): Executing check '/sbin/swapon -s | /bin/grep file > /dev/null'
Debug: Executing '/sbin/swapon -s | /bin/grep file > /dev/null'
Debug: Exec[makeswap](provider=posix): Executing 'mkswap /swapfile'
Debug: Executing 'mkswap /swapfile'
Notice: /Stage[main]/Swap/Exec[makeswap]/returns: executed successfully
...

答案1

通过在以下语句中添加以下语句,该问题已得到解决

除非 => “/sbin/swapon -s | /bin/grep 文件 > /dev/null”

相关内容