Skip to main content

Chef Infra Language: Editing Resources

declare_resource

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) or :template (for the template resource). Any resource available to Chef may be declared.
  • resource_name the property that’s 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

delete_resource

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) or :template (for the template resource). Any resource available to Chef may be declared.
  • resource_name the property that’s 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')

delete_resource!

Use the delete_resource! method to find a resource in the resource collection, and then delete it. If the resource isn’t 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) or :template (for the template resource). Any resource available to Chef Infra may be declared.
  • resource_name the property that’s 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')

edit_resource

Use the edit_resource method to:

  • Find a resource in the resource collection, and then edit it.
  • Define a resource block. If a resource block with the same name exists in the resource collection, it will be updated with the contents of the resource block defined by the edit_resource method. If a resource block doesn’t 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) or :template (for the template resource). Any resource available to Chef may be declared.
  • resource_name the property that’s 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

edit_resource!

Use the edit_resource! method to:

  • Find a resource in the resource collection, and then edit it.
  • Define a resource block. If a resource with the same name exists in the resource collection, its properties will be updated with the contents of the resource block defined by the edit_resource method.

In both cases, if the resource isn’t 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) or :template (for the template resource). Any resource available to Chef may be declared.
  • resource_name the property that’s 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')

find_resource

Use the find_resource method to:

  • Find a resource in the resource collection.
  • Define a resource block. If a resource block with the same name exists in the resource collection, it will be returned. If a resource block doesn’t exist in the resource collection, it will be created.

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) or :template (for the template resource). Any resource available to Chef may be declared.
  • resource_name the property that’s 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

find_resource!

Use the find_resource! method to find a resource in the resource collection. If the resource isn’t 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) or :template (for the template resource). Any resource available to Chef may be declared.
  • resource_name the property that’s 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')
Edit this page on GitHub

Thank you for your feedback!

×