【问题标题】:Using multiple conditions in Chef only_if guard在 Chef only_if 中使用多个条件
【发布时间】:2017-03-01 05:28:45
【问题描述】:

我正在使用以下厨师资源:

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

如果 only_if 防护使用正确,请告诉我

【问题讨论】:

  • 更多only_if %w(6 5).include?(node['platform_version')。这只是一个 ruby​​ 测试,所以如果你想做一个 X == 5 或 6,最好的办法是创建一个 [5,6] 数组并测试这个数组是否包含实际的 platform_version 值。
  • 如果平台版本是6.5,你认为它可能会给出错误的结果。我应该改为only_if %w(6 5).include?(node['platform_version'].to_i)
  • 6.5 当用 to_i 调用时会返回 6,所以是的,它会更好。

标签: ruby chef-infra chef-recipe chef-solo cookbook


【解决方案1】:

保护块中的 ruby​​ 命令应包含在 {} 中。另请注意,保护块内的任何 ruby​​ 代码都将在收敛阶段(即运行时)执行。我有一个类似的场景,根据上面 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

所以对于你的场景,我相信保护块会像

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

【讨论】:

    猜你喜欢
    • 2017-01-27
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2015-01-18
    • 2014-11-12
    • 1970-01-01
    相关资源
    最近更新 更多