How can you access Chef LWRP attributes in a recipe

左心房为你撑大大i 提交于 2019-12-11 13:48:54

问题


With some of the default chef resources, it is possible to access some of their attributes after they have been called

# 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"

How can this be achieved in a custom LWRP (here is an example)

# resources/default.rb
actions :create
default_action :create

attribute :relative_path, :kind_of => String, :name_attribute => true
attribute :full_path, :kind_of => String

In this provider, I am trying to update the property of new_resource.full_path to be equal to the path of the file resource

# 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

However when I try to access resource.full_path in the recipe, it is nil rather than the expected /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:"

This example is rather contrived I know, the real world application/reason for this can be seen in the default resource/provider here https://github.com/peterjmit/chef-ssl-cert

来源:https://stackoverflow.com/questions/25043690/how-can-you-access-chef-lwrp-attributes-in-a-recipe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!