Chef: resource basics

Example first:

We are firmly told not to use Notepad.exe for editing… so we’ll use Notepad++… as we would anyway, most likely.

I have a file [create_users.rb], containing this:


Which says: the resource type is [file], the resource name is [d:\create_users.txt]. As no [action] is specified, I’ll do the default action for a [file], which is [create]. And the file’s content is to be [Dennis].

This is the command line:

chef-apply D:\create_users.rb

, and the run, including the content of the generated file:


So the .rb file specified a simple Desired State, i.e. create the file, give it some content. What happens if we edit the content file, and re-run the rb file?



The file has been overwritten based on the Desired State rules:


If we now revert the current content back to the manual edit:


, and update the rb file to remove the content rule…


, and re-run the Chef command:


, then because we only asked it to ensure that the file was created (default), then the content remains as-is, and when I move the focus back to Notepad++, I do not get a prompt to say the file has been changed/reloaded. This is probably because Resources use the Test/Repair pattern: Test to see if the resource is in the Desired State. If No, Repair, else return.


There is good documentation on the File Resource here. Getting access rights often feels like the trickiest challenge, so it will be interesting to see how Chef can facilitate that… not needed right now.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s