2017-02-09 97 views
0

我有限定的默认属性:厨师属性重写未生效

/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 

下面介绍如何将R ecipe(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

最后发现我的问题,问题是我试图覆盖ruby_block内部的属性,它比我想象的更晚执行。

我删除了红宝石块,以强制厨师运行它,它解决了我的问题。

+3

厨师确保订单,这不仅仅是您认为的顺序。请参阅https://coderanger.net/two-pass获取概述:) – coderanger

+0

感谢您的链接:) – david