【问题标题】:Referencing attributes from within a Chef recipe从 Chef 配方中引用属性
【发布时间】:2017-02-10 06:53:15
【问题描述】:

我有一个名为default.rb 的属性文件,其中有一个键default['current'],它以字符串格式存储一个目录。我想在更改目录时将其用于以下命令:

# Run database migrations
execute 'database migrations' do
  user    'ubuntu'
  cwd     "#{default['current']}"
  command 'sudo php artisan down && sudo php artisan migrate --force && sudo php artisan up'
end

当我运行配方时,我得到了以下错误。

NameError
---------
No resource, method, or local variable named `default' for `Chef::Recipe "deploy"'

有没有我想念的正确方法来做到这一点?我宁愿不在工作目录中硬编码。

【问题讨论】:

    标签: ruby chef-infra chef-recipe string-interpolation chef-attributes


    【解决方案1】:

    当在配方中引用属性时,我们需要在它们前面加上node,如下所示:

    cwd     node['current']
    

    另请参阅:Overriding attributes in the recipe

    【讨论】:

    • 在配方中,node["variable"] 不一定获得默认值
    • @JonMalachowski 你愿意详细说明吗?
    • 特指默认级别,是可以设置属性数据的10个级别之一。
    • 另外,单个值也不需要引号
    • chef 中的符号和字符串实际上没有区别——只是为了便于阅读而保持一致。符号少了一个字符:-)
    【解决方案2】:

    这似乎得到了回答。但我没有看到此文档核心的链接(IMO) 都在这里:https://docs.chef.io/attributes.html

    【讨论】:

    • 我很欣赏这一点,但它也让我回到了我在确认答案中提出的问题。我曾认为default['attribute']node.default['attribute'] 相同,并且根据文档,如果不包括node,则暗示node,那么为什么我需要包括node 才能引用该属性?作为记录,我能够使用 node.default['attribute'] 完成引用,这是在更新为 node['attribute'] 之前给出的答案。
    • 在配方中使用变量(读取)时,您应该使用'node["attribute"]',这允许在环境/角色/cli等中覆盖变量属性在环境/变量/配方默认值中定义,您可以选择定义它的级别。在运行时,通过按连续顺序加载它们来计算最终节点属性哈希图 [在该链接中列出]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    相关资源
    最近更新 更多