1.

How to create a custom resource in Chef?

Answer»

Custom RESOURCES in Chef are add on resources transported as a pat of cookbooks and used by Chef Client. These are reusable in the same way as pre-defined resources in Chef. Custom resources are kept as a separate Ruby FILE in the resources folder of a cookbook. For eg: Assume that we need to create a custom resource called “WEBSITE” that make use of in-built resources of Chef like file, package and service resources.
“website.rb “ file looks as follows:

property :home, String, default: '<h1>Welcome!</h1>' action: create do   package 'nginx'   service 'nginx' do action [:enable, :start]   end   file '/var/www/html/index.html' do content new_res.homepage   end end action :delete do   package 'nginx' do action :delete   end end

Where

  • “Home” is the property that sets the default value “Welcome!” for index.html file.
  • The new resource has two actions, to create and delete. Create is the default action.

The website resource is NAMED after the cookbook NAME and the file name in the resources folder. Hence if the name of our cookbook is “sample” our custom resource is defined as “sample_website” and could be used in the cookbook as follows:

sample_website 'nginx' do   home '<h1>Welcome to the Sample  website!</h1>' end


Discussion

No Comment Found