0

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

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

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

    source "#{@new_resource.vu_name}"

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

    source "text.txt"

并获取了文件。对我来说,看起来cookbook_file内部 ruby​​ 块没有得到副本,new_resource它变成nil.

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

4

1 回答 1

0

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

于 2016-02-24T02:13:07.170 回答