2015-09-04 69 views
1

我有一個角色B的節點。該角色在default_attributes部分中有一些屬性。廚師食譜沒有從連接到節點的角色獲取屬性

{ 
    "name": "B", 
    "description": "", 
    "json_class": "Chef::Role", 
    "default_attributes": { 
    "service_name": "my_value" 
    }, 
    "override_attributes": { 
    }, 
    "chef_type": "role", 
    "run_list": [ 
    ], 
    "env_run_lists": { 
    } 
} 

在我這個節點上執行與角色B中的食譜,我想借此值與連接到該節點是角色B中的關鍵service_name

使用node.default['service_name']node['service_name']該值不存在。我如何獲得這個價值?

例子:

在名爲sample食譜我有一個名爲install配方,所以在install.rb這一行: Chef::Log.info("my service_name is " + node.default['service_name'])這使我的下一個錯誤:

172.26.67.100 ================================================================================ 
172.26.67.100 Recipe Compile Error in /var/chef/cache/cookbooks/sample/recipes/install.rb 
172.26.67.100 ================================================================================ 
172.26.67.100 
172.26.67.100 TypeError 
172.26.67.100 --------- 
172.26.67.100 can't convert Chef::Node::VividMash to String (Chef::Node::VividMash#to_str gives Chef::Node::VividMash) 

... 
... 
... 

172.26.67.100 Running handlers: 
172.26.67.100 [2015-09-04T15:58:30+02:00] ERROR: Running exception handlers 
172.26.67.100 Running handlers complete 
172.26.67.100 [2015-09-04T15:58:30+02:00] ERROR: Exception handlers complete 
172.26.67.100 [2015-09-04T15:58:30+02:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out 
172.26.67.100 Chef Client failed. 0 resources updated in 2.575100287 seconds 
172.26.67.100 [2015-09-04T15:58:30+02:00] ERROR: can't convert Chef::Node::VividMash to String (Chef::Node::VividMash#to_str gives Chef::Node::VividMash) 
172.26.67.100 [2015-09-04T15:58:30+02:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1) 
+0

我已通過提供一些日誌更新了該文章。 –

+2

'node.default'訪問節點對象的特定部分,節點的缺省值(即從屬性文件或配方填充的值)決不會使用該級別訪問節點對象,只能設置它們。然後串聯不是用ruby中的+完成的。使用插值:'Chef :: Log.info(「我的service_name是#{node ['service_name']}」)'。 – Tensibai

+0

node ['service_name']的值是一個空字符串,與我的角色不同。我已經使用'knife role edit my-role'更新了角色。我錯過了什麼? –

回答

0

這是我的錯誤。我發現如果你想從食譜食譜中的某個角色處取得default_attributes,你必須在角色運行列表中運行該配方,但我沒有這樣做。

因此,我不得不跑本身,chef-client -o recipe[B],並把sample cookbook在B角色的run_list