【问题标题】:How to use Chef DSL inside LWRP's action如何在 LWRP 的操作中使用 Chef DSL
【发布时间】:2016-02-24 10:13:07
【问题描述】:

我有 LWRP,它应该在其 create 操作中创建 cookbook_file

resource_name :vuy
property :vu_name, String, name_property :true
actions :create

action :create do
  log "vuy:@new_resource.vu_name-#{@new_resource.vu_name}"
  cookbook_file "c:/temp/test.xml" do
    source "#{@new_resource.vu_name}"
  end
end

和测试配方

vuy 'text.txt'

chef-client 执行失败,错误为NoMethodError: undefined method 'vu_name' for nil:NilClass

当我从 create 方法中删除 cookbook_file 时,日志正确显示:INFO: vu:@new_resource.vu_name-text.txt 在下一步中,我替换了

    source "#{@new_resource.vu_name}"

与测试配方中指定的值相同

    source "text.txt"

并且文件被提取。 在我看来,cookbook_file 内部 ruby​​ 块没有得到new_resource 的副本,它变成了nil

如何使用 LWRP 的属性作为在 action 中声明的资源的参数?

【问题讨论】:

  • 根据您的语法,您使用的是 chef 12.5+ 自定义资源,只需按照 the doc here 删除 @new_resource 部分
  • 就是这样。虽然我读了那个文件,但我还是错过了 :-)

标签: ruby chef-infra lwrp


【解决方案1】:

应该是new_resource.vu_name,即没有@。使用 at 符号是局部变量查找,没有它是方法调用。 Chef 会自动提升方法调用,因此即使在 cookbook_file 内您也能得到正确的结果,但它无法通过实例变量查找来做到这一点。

【讨论】:

    猜你喜欢
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多