Use the declare_resource method to instantiate a resource and then add it to the resource collection.
The syntax for the declare_resource method is as follows:
declare_resource(:resource_type, 'resource_name', resource_attrs_block)
where:
:resource_type is the resource type, such as :file (for the file resource), :template (for the template resource), and so on. Any resource available to Chef may be declared.resource_name the property that is the default name of the resource, typically the string that appears in the resource 'name' do block of a resource (but not always); see the Syntax section for the resource to be declared to verify the default name property.resource_attrs_block is a block in which properties of the instantiated resource are declared.For example:
declare_resource(:file, '/x/y.txy', caller[0]) do
action :delete
end
is equivalent to:
file '/x/y.txt' do
action :delete
end
Use the delete_resource method to find a resource in the resource collection, and then delete it.
The syntax for the delete_resource method is as follows:
delete_resource(:resource_type, 'resource_name')
where:
:resource_type is the resource type, such as :file (for the file resource), :template (for the template resource), and so on. Any resource available to Chef may be declared.resource_name the property that is the default name of the resource, typically the string that appears in the resource 'name' do block of a resource (but not always); see the Syntax section for the resource to be declared to verify the default name property.For example:
delete_resource(:template, '/x/y.erb')
Use the delete_resource! method to find a resource in the resource collection, and then delete it. If the resource is not found, an exception is returned.
The syntax for the delete_resource! method is as follows:
delete_resource!(:resource_type, 'resource_name')
where:
:resource_type is the resource type, such as :file (for the file resource), :template (for the template resource), and so on. Any resource available to Chef Infra may be declared.resource_name the property that is the default name of the resource, typically the string that appears in the resource 'name' do block of a resource (but not always); see the Syntax section for the resource to be declared to verify the default name property.For example:
delete_resource!(:file, '/x/file.txt')
Use the edit_resource method to:
edit_resource method. If a resource block does not exist in the resource collection, it will be created.The syntax for the edit_resource method is as follows:
edit_resource(:resource_type, 'resource_name', resource_attrs_block)
where:
:resource_type is the resource type, such as :file (for the file resource), :template (for the template resource), and so on. Any resource available to Chef may be declared.resource_name the property that is the default name of the resource, typically the string that appears in the resource 'name' do block of a resource (but not always); see the Syntax section for the resource to be declared to verify the default name property.resource_attrs_block is a block in which properties of the instantiated resource are declared.For example:
edit_resource(:template, '/x/y.txy') do
cookbook 'cookbook_name'
end
and a resource block:
edit_resource(:template, '/etc/aliases') do
source 'aliases.erb'
cookbook 'aliases'
variables({:aliases => {} })
notifies :run, 'execute[newaliases]'
end
Use the edit_resource! method to:
edit_resource method.In both cases, if the resource is not found, an exception is returned.
The syntax for the edit_resource! method is as follows:
edit_resource!(:resource_type, 'resource_name')
where:
:resource_type is the resource type, such as :file (for the file resource), :template (for the template resource), and so on. Any resource available to Chef may be declared.resource_name the property that is the default name of the resource, typically the string that appears in the resource 'name' do block of a resource (but not always); see the Syntax section for the resource to be declared to verify the default name property.resource_attrs_block is a block in which properties of the instantiated resource are declared.For example:
edit_resource!(:file, '/x/y.rst')
Use the find_resource method to:
The syntax for the find_resource method is as follows:
find_resource(:resource_type, 'resource_name')
where:
:resource_type is the resource type, such as :file (for the file resource), :template (for the template resource), and so on. Any resource available to Chef may be declared.resource_name the property that is the default name of the resource, typically the string that appears in the resource 'name' do block of a resource (but not always); see the Syntax section for the resource to be declared to verify the default name property.For example:
find_resource(:template, '/x/y.txy')
and a resource block:
find_resource(:template, '/etc/seapower') do
source 'seapower.erb'
cookbook 'seapower'
variables({:seapower => {} })
notifies :run, 'execute[newseapower]'
end
Use the find_resource! method to find a resource in the resource collection. If the resource is not found, an exception is returned.
The syntax for the find_resource! method is as follows:
find_resource!(:resource_type, 'resource_name')
where:
:resource_type is the resource type, such as :file (for the file resource), :template (for the template resource), and so on. Any resource available to Chef may be declared.resource_name the property that is the default name of the resource, typically the string that appears in the resource 'name' do block of a resource (but not always); see the Syntax section for the resource to be declared to verify the default name property.For example:
find_resource!(:template, '/x/y.erb')
© Chef Software, Inc.
Licensed under the Creative Commons Attribution 3.0 Unported License.
The Chef™ Mark and Chef Logo are either registered trademarks/service marks or trademarks/servicemarks of Chef, in the United States and other countries and are used with Chef Inc's permission.
We are not affiliated with, endorsed or sponsored by Chef Inc.
https://docs.chef.io/infra_language/editing_resources/