使用一些默认的Chef资源,可以在调用它们之后访问它们的一些属性
# recipes/default.rb
f = file "/tmp/file_resource" do
owner "root"
group "root"
mode "0755"
action :create
end
log "Path to my file is #{f.path}" # outputs "/tmp/file_resource"
如何在自定义LWRP中实现(这是一个示例)
# resources/default.rb
actions :create
default_action :create
attribute :relative_path, :kind_of => String, :name_attribute => true
attribute :full_path, :kind_of => String
在此提供程序中,我尝试将new_resource.full_path
的属性更新为等于文件资源的路径
# providers/default.rb
action :create do
f = file "/path/to/my/resource/#{new_resource.relative_path}" do
owner "root"
group "root"
mode "0755"
action :create
end
new_resource.full_path(f.path)
new_resource.updated_by_last_action(f.updated_by_last_action?)
end
但是,当我尝试访问食谱中的resource.full_path
时,它是nil
而不是预期的/path/to/my/resource/relative/path
# recipes/default.rb
resource = my_awesome_lwrp "relative/path" do
action :create
end
log "Full path for my resource: #{resource.full_path}" # outputs "Full path for my resource:"
我知道这个例子非常人为,真实世界的应用程序/原因可以在这里的默认资源/提供者https://github.com/peterjmit/chef-ssl-cert
中看到