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:

CreateUsersRb01

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:

CreateUsersRb02

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?

CreateUsersRb03

CreateUsersRb04

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

CreateUsersRb05

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

CreateUsersRb03

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

CreateUsersRb06

, and re-run the Chef command:

CreateUsersRb07

, 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.

CreateUsersRb03

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.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s