【问题标题】:Chef attribute override not taking effect厨师属性覆盖不生效
【发布时间】:2017-02-12 04:52:30
【问题描述】:

我定义了一个默认属性:

/cookbook-test/attributes/default.rb
## Environemnt attributes
default['cookbook-test']['flags']['test']="dummy_flag" 

在配方 (1) 上被覆盖并在配方 (2) 上使用

/cookbook-test/recipes/default.rb
include_recipe 'cookbook-test::set-flag-based-on-file' #(1)
#...
include_recipe 'cookbook-test::other-recipe' # (2) this recipe needs the attribute

在属性被覆盖的配方 (1) 中,属性会根据文件的内容而改变:

/cookbook-test/recipes/set-flag-based-on-file.rb
ruby_block "Use attribute (A/B)" do
    block do
        # reads file for flag "A"/"B"
        local_flag= ::File.read('/home/user/.flag').chomp

        if local_flag== "A"
          node.set['cookbook-test']['flags']['test'] = true 
        elsif local_flag== "B"
          node.set['cookbook-test']['flags']['test'] = false
        else
          node.set['cookbook-test']['flags']['test'] = false
        end
    end
end

配方 (2) 尝试使用该属性的方式如下:

/cookbook-test/recipes/other-recipe.rb
flag= node['cookbook-test']['flags']['test']
if flag == true
  # something 
elsif flag == false
  # something
else
  Chef::Log.error("XX attribute is not SET!")
end

但是,当食谱运行并进入配方 (2) 时,该值仍设置为“dummy_flag”,因为它从未被覆盖。我尝试使用“node.force_default”设置属性,但没有成功。

即使文件被正确读取,带有“A”标志的文件也没有效果......

var/chef/log/client.log
...
[2017-02-09T12:56:06-05:00] ERROR: XX attribute is not SET!
...

为了正确覆盖属性,我缺少什么?

【问题讨论】:

    标签: chef-infra chef-recipe


    【解决方案1】:

    终于找到了我的问题,问题是我试图覆盖 ruby​​_block 中的属性,该属性的执行时间比我想象的要晚。

    我删除了 ruby​​ 块以强制厨师早点运行它,它解决了我的问题。

    【讨论】:

    • 厨师确实保证了订单,只是不是您认为的订单。有关概述,请参阅coderanger.net/two-pass :)