【问题标题】:What are attribute methods of a Chef LWRP and what can I pass them?Chef LWRP 的属性方法是什么,我可以传递什么?
【发布时间】:2017-02-10 07:31:06
【问题描述】:

我在厨师资源中有以下属性:

attribute :attribName, kind_of: String, name_attribute: true, default: 'big string sldkjslkdflksdksdlkff'

我想打破它,让它看起来不错,所以我这样做了:

attribute [
  :attribName,
  kind_of: String,
  name_attribute: true,
  default: 'big string sldkjslkdflksdksdlkff'
]

但在收敛时出现错误:

   NoMethodError
   -------------
   undefined method `to_sym' for #<Array:0x00000004a63b60>
   Did you mean?  to_s
           to_yaml
           to_set

   Platform:
   ---------
   x64-mingw32    

   Running handlers:
   [2016-10-01T19:07:39-07:00] ERROR: Running exception handlers
   Running handlers complete
   [2016-10-01T19:07:39-07:00] ERROR: Exception handlers complete
   Chef Client failed. 0 resources updated in 11 seconds
   [2016-10-01T19:07:39-07:00] FATAL: Stacktrace dumped to C:/Users/ADMINI~1/AppData/Local/Temp/kitchen/cache/chef-stacktrace.out
   [2016-10-01T19:07:39-07:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
   [2016-10-01T19:07:39-07:00] FATAL: NoMethodError: undefined method `to_sym' for #<Array:0x00000004a63b60>
   Did you mean?  to_s
           to_yaml
           to_set

所以我认为资源文件中的attributes 只是接受参数数组并将 [..args..] 传递给它的方法。为什么这不起作用?我想我对这种上下文中的 ruby​​ 对象属性的类型以及它们的行为感到困惑。

【问题讨论】:

    标签: ruby chef-infra chef-recipe cookbook lwrp


    【解决方案1】:

    attribute 方法试图符号化第一个参数,它是属性的名称。第二个参数看起来像是选项的散列,所以方法签名应该是这样的:def attribute(name, options={})。当您将所有内容包裹在括号内时,您发送了一个数组作为第一个参数。

    尝试像这样重新格式化它:

    attribute :attribName,
      kind_of: String,
      name_attribute: true,
      default: 'big string sldkjslkdflksdksdlkff'
    

    如果你真的不喜欢第一个参数,你可以使用 splat(我根本不喜欢这个):

    attribute *[
      :attribName,
      {
        kind_of: String,
        name_attribute: true,
        default: 'big string sldkjslkdflksdksdlkff'
      }
    ]
    

    这会将数组元素转换为参数。

    【讨论】:

    • 除非您使用的是真正旧版本的 Chef,否则请使用 property 而不是 attribute。后者仅适用于向后兼容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2021-09-07
    相关资源
    最近更新 更多