Search code examples
chef-infra

How to use a Chef custom resource name as a variable?


If we are defining a resource such that it will be used as such:

custom_resource 'name' do  
  some_property some_value
  action :create
end

Is there a way I can use name as a variable in my custom resource definition? Similar to how the file resource uses the name as a path.

file '/var/www/myfile.php' do
  content '<html>Some HTML</html>'
  mode '0755'
  owner 'web_admin'
  group 'web_admin'
end

Solution

  • By default all the custom resources have the name property which you can access

    new_resource.name
    

    If you need some other property to become name property, as file resource has path property as name, you must add name_property: true to its declaration in your resource like that:

    property :some_property, String, name_property: true
    

    https://docs.chef.io/custom_resources.html#define-properties