Chef属性覆盖未生效

时间:2017-02-09 17:56:49

标签: chef chef-recipe

我定义了默认属性:

/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!
...

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

1 个答案:

答案 0 :(得分:0)

终于找到了我的问题,问题是我试图覆盖ruby_block中的属性,这比我想象的要晚。

我删除了ruby块以强制厨师更早地运行它并解决了我的问题。