如何使用角色覆盖Chef中的默认属性

时间:2015-04-01 14:04:36

标签: chef

我无法让它工作,在这里工作了好几个小时。这里有一个重复的问题,但它从来没有得到充分的回答。我试图将问题降到最低。

这是我的琐碎食谱:“my_recipe.rb”

file "/tmp/my_target_file" do
  content node.default['my_file_content']
  action :create
end

...和attributes子目录中的属性文件:“my_attribute.rb”

default['my_file_content'] = 'From my_attribute.rb'

我在roles子目录中创建了一个角色:“my_role.rb”

name 'my_role'
description 'My role'
run_list 'recipe[my_cookbook::my_recipe]'
default_attributes['my_file_content'] = 'From my_role.rb'

我将cookbook和角色上传到我的Chef服务器并从我的厨师工作站运行一把刀命令:

knife bootstrap xxx.xxx.xxx.xxx -x myusername -P mysudopassword --sudo -r role[my_role] -N my_node_name

它运行正常并且创建了目标文件,但是/ tmp / my_target_file的内容不是我所期望的。我正在

$ cat /tmp/my_target_file
From my_attribute.rb

......但当然我想要的是

$ cat /tmp/my_target_file
From my_role.rb

根据Chef属性优先级http://docs.chef.io/attributes.html,这应该有效。我也试过使用'override_attributes'但得到相同的结果。如果我登录到我的Chef服务器并深入查看我的节点的属性,我可以看到属性

my_file_content: From my_role.rb

与其他Ohai生成的一起。

显然我没有到达这里。有人可以解释如何使用角色覆盖属性吗?

1 个答案:

答案 0 :(得分:3)

file "/tmp/my_target_file" do
  content node.default['my_file_content']
  action :create
end

node.default是对节点对象的方法调用,旨在更新配方中['my_file_content']属性的值。

它不应该用于获取值,因为您正在获取默认配方级别的值(即:来自属性文件),而不是来自属性优先级的结果值。

而是以这种方式使用node['my_file_content'],您可以询问my_file_content属性的值,该属性来自不同的优先级。

像这样使用它会起作用:

file "/tmp/my_target_file" do
  content node['my_file_content']
  action :create
end
相关问题