I have a cookbook my_service
with a custom resource write_config
.
This resource is called from another cookbook node_a
.
my_service/resources/write_config.rb
:
property :template_source, String, name_property: true
property :calling_cookbook, String, required: true
action :create do
template "/tmp/test.txt" do
source template_source
cookbook calling_cookbook
owner 'root'
mode '0644'
action :create
end
end
Node cookbook recipe node_a/default.rb
:
my_service_write_config 'config.erb' do
calling_cookbook 'node_a'
end
This works fine, but I would like to get rid of the calling_cookbook
property.
Is there a way to automatically get the calling_cookbook
name?
(Thank you coderanger!)
The basic resource template
seems to get evaluated in the context of the calling (node_a
) cookbook (?).
my_service/resources/write_config.rb
:
property :template_source, String, name_property: true
action :create do
template "/tmp/test.txt" do
source template_source
owner 'root'
mode '0644'
action :create
end
end
Using it within the node cookbook recipe node_a/default.rb
becomes a one-liner:
...
include_recipe 'my_service'
my_service_write_config 'config.erb'
...
Neat!
This is tracked automatically as new_resource.cookbook_name
. You may need to to_s
it because somehow it ends up a symbol sometimes but otherwise should be what you need.
Also specifically for templates this is already the default behavior if you just remove the cookbook
line.