简体   繁体   English

在Chef only_if guard中使用多个条件

[英]Using multiple conditions in Chef only_if guard

I am using the following chef resource: 我使用以下主厨资源:

execute 'run_command' do
  command "chkconfig service on"
  only_if node['platform_version'].to_i == 6, 5
end

Please let me know if only_if guard is used in correct fashion 如果only_if guard以正确的方式使用,请告诉我

The ruby commands in the guard block should be enclosed in {}. 保护块中的ruby命令应该包含在{}中。 Also note that any ruby code inside the guard block will be executed at the converging phase ie run-time. 另请注意,保护块内的任何ruby代码都将在收敛阶段(即运行时)执行。 I had a similar scenario and based on tensibai's comment above, I just used a array with pre-included values to be checked for the ubuntu version. 我有一个类似的场景,基于上面的tensibai的评论,我只是使用一个包含预先包含的值的数组来检查ubuntu版本。

execute "apt-get-update" do
  command "apt-get update"
  ignore_failure true
  notifies :install, 'package[ntp]', :immediately
  not_if {[16.04, 18.04].include?(node['platform_version'].to_f) && (node['packages'].keys.include? "ntp")}
  #not_if {(node['packages'].keys.include? "ntp")}
end

So for your scenario, I believe that the guard-block would be something like 所以对于你的场景,我相信守卫块会是这样的

execute 'run_command' do
    command "chkconfig service on"
    only_if {[5, 6].include?(node['platform_version'].to_i)} 
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM