Chef:在不同的食谱中使用属性

时间:2015-10-21 17:50:17

标签: chef chef-solo

我试图在另一本食谱中使用一本食谱中定义的属性,比如foo,比如说吧。我为nil得到了错误“undefined method` []':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]"]
}

富/属性/ default.rb

default['foo']['var1'] = "Hello"

酒吧/属性/ default.rb

default['bar']['var1'] = node['foo']['var1']

富/ 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是我的申请,barChef Maven。如何导入属性??

1 个答案:

答案 0 :(得分:3)

您的菜谱依赖性已被颠倒。如果你想在酒吧食谱中使用node['foo']['var1']属性,那么酒吧食谱应该取决于foo食谱,而不是相反。

摆脱 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

<强>食谱/富/属性/ default.rb

default['foo']['var1'] = "Hello"

<强>食谱/酒吧/属性/ default.rb

default['bar']['var1'] = node['foo']['var1']

<强>食谱/酒吧/食谱/ default.rb

log "The value of ['bar']['var1'] is #{node['bar']['var1']}. The value of ['foo']['var1'] is also #{node['foo']['var1']}"

<强>食谱/酒吧/ 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