厨师:在不同的食谱中使用属性
Chef: Use attribute in different cookbook
我正在尝试使用在一本食谱中定义的属性,比如 foo,在另一本食谱中,比如 bar。我收到错误 "undefined method `[]' for nil:NilClass"
Chef
solo.rb
cookbooks
foo
attributes
default.rb
recipes
default.rb
metadata.rb
bar
attributes
default.rb
recipes
default.rb
metadata.rb
roles
roles.json
以上是我大厨的结构
solo.rb
cookbook_path File.expand_path("../chef/cookbooks", __FILE__)
json_attribs File.expand_path("../chef/roles/roles.json", __FILE__)
log_location STDOUT
log_level :debug
roles.json
{
"run_list": ["recipe[foo::default]"]
}
foo/attributes/default.rb
default['foo']['var1'] = "Hello"
bar/attributes/default.rb
default['bar']['var1'] = node['foo']['var1']
foo/metadata.rb
name 'foo'
depends 'bar'
当我运行查询chef-solo -c solo.rb
时,我得到如下错误
================================================================================
Recipe Compile Error in ../chef/cookbooks/bar/attributes/default.rb
================================================================================
NoMethodError
-------------
undefined method `[]' for nil:NilClass
Relevant File Content:
----------------------
Recipe Compile Error in ../chef/cookbooks/bar/attributes/default.rb:
1>> default['bar']['var1'] = node['foo']['var1']
这里foo
是我的申请,bar
是Chef Maven。如何导入属性??
您已反转食谱依赖项。如果你想在 bar cookbook 中使用 node['foo']['var1']
属性,那么 bar cookbook 应该依赖于 foo cookbook,而不是相反。
删除foo/metadata.rb中的depends 'bar'
并创建bar/metadata.rb
name 'bar'
depends 'foo'
完整示例:
|____cookbooks
| |____foo
| | |____attributes
| | | |____default.rb
| |____bar
| | |____recipes
| | | |____default.rb
| | |____attributes
| | | |____default.rb
| | |____metadata.rb
cookbooks/foo/attributes/default.rb
default['foo']['var1'] = "Hello"
cookbooks/bar/attributes/default.rb
default['bar']['var1'] = node['foo']['var1']
cookbooks/bar/recipes/default.rb
log "The value of ['bar']['var1'] is #{node['bar']['var1']}. The value of ['foo']['var1'] is also #{node['foo']['var1']}"
食谱/bar/metadata.rb
name 'bar'
depends 'foo'
示例输出
# chef-client -z -o 'recipe[bar]'
...
resolving cookbooks for run list: ["bar"]
Synchronizing Cookbooks:
- foo (0.0.0)
- bar (0.0.0)
Compiling Cookbooks...
Converging 1 resources
Recipe: bar::default
* log[The value of ['bar']['var1'] is Hello. The value of ['foo']['var1'] is also Hello] action write
我正在尝试使用在一本食谱中定义的属性,比如 foo,在另一本食谱中,比如 bar。我收到错误 "undefined method `[]' for nil:NilClass"
Chef
solo.rb
cookbooks
foo
attributes
default.rb
recipes
default.rb
metadata.rb
bar
attributes
default.rb
recipes
default.rb
metadata.rb
roles
roles.json
以上是我大厨的结构
solo.rb
cookbook_path File.expand_path("../chef/cookbooks", __FILE__)
json_attribs File.expand_path("../chef/roles/roles.json", __FILE__)
log_location STDOUT
log_level :debug
roles.json
{
"run_list": ["recipe[foo::default]"]
}
foo/attributes/default.rb
default['foo']['var1'] = "Hello"
bar/attributes/default.rb
default['bar']['var1'] = node['foo']['var1']
foo/metadata.rb
name 'foo'
depends 'bar'
当我运行查询chef-solo -c solo.rb
时,我得到如下错误
================================================================================
Recipe Compile Error in ../chef/cookbooks/bar/attributes/default.rb
================================================================================
NoMethodError
-------------
undefined method `[]' for nil:NilClass
Relevant File Content:
----------------------
Recipe Compile Error in ../chef/cookbooks/bar/attributes/default.rb:
1>> default['bar']['var1'] = node['foo']['var1']
这里foo
是我的申请,bar
是Chef Maven。如何导入属性??
您已反转食谱依赖项。如果你想在 bar cookbook 中使用 node['foo']['var1']
属性,那么 bar cookbook 应该依赖于 foo cookbook,而不是相反。
删除foo/metadata.rb中的depends 'bar'
并创建bar/metadata.rb
name 'bar'
depends 'foo'
完整示例:
|____cookbooks
| |____foo
| | |____attributes
| | | |____default.rb
| |____bar
| | |____recipes
| | | |____default.rb
| | |____attributes
| | | |____default.rb
| | |____metadata.rb
cookbooks/foo/attributes/default.rb
default['foo']['var1'] = "Hello"
cookbooks/bar/attributes/default.rb
default['bar']['var1'] = node['foo']['var1']
cookbooks/bar/recipes/default.rb
log "The value of ['bar']['var1'] is #{node['bar']['var1']}. The value of ['foo']['var1'] is also #{node['foo']['var1']}"
食谱/bar/metadata.rb
name 'bar'
depends 'foo'
示例输出
# chef-client -z -o 'recipe[bar]'
...
resolving cookbooks for run list: ["bar"]
Synchronizing Cookbooks:
- foo (0.0.0)
- bar (0.0.0)
Compiling Cookbooks...
Converging 1 resources
Recipe: bar::default
* log[The value of ['bar']['var1'] is Hello. The value of ['foo']['var1'] is also Hello] action write