Creating your own custom resource
Chef offers the opportunity to extend the list of available resources by creating a custom resource. By creating your own custom resources, you can simplify writing cookbooks because your own custom resources enrich the Chef DSL and make your recipe code more expressive.
In this section, we will create a very simple custom resource to demonstrate the basic mechanics.
Getting ready
Create a new cookbook named greeting
and ensure that the run_list
of your node includes greeting
, as described in the Creating and using cookbooks recipe of Chapter 1, Chef Infrastructure.
How to do it…
Let's see how to build a very simple custom resource to create a text file on your node:
Create the custom resource in your
greeting
cookbook:mma@laptop:~/chef-repo $ subl cookbooks/greeting/resources/file.rb property :title, String, default: "World" property :path, String, default: "/tmp/greeting.txt" action :create do Chef::Log.info "Adding '#{new_resource.name}' greeting as #{new_resource...