Information Technology Reference
In-Depth Information
Introducing Idempotence
Although the recipe we created in
Example A-3
is a good first attempt, it is not
idempotent
.
When Chef code is idempotent, it can be run multiple times on the same system and the res-
ults will always be identical, without producing unintended side effects. All Chef default re-
sources are guaranteed to be
idempotent
with the exception of the
execute
resource.
execute
resources generally are not idempotent, because most command-line utilities can be
run only once. They assume that a human being is interacting with the system and under-
stands the state of the system. For example, assuming the file
/learningchef/file1.txt
exists,
the following
mv
command will work the first time it is run, but it will fail the second time:
$
mv /learningchef/file1.txt /file1.txt
A great way to test to see if your recipe is idempotent is to run
kitchen converge
twice
.
When a recipe has no unintended side effects, there should be 0 resources updated on the
second run.
Does our recipe pass the idempotency test? Sadly, no. Here's a sampling of the output from
an initial
kitchen converge
:
$
kitchen converge default-centos65
-----> Starting Kitchen (v1.2.2.dev)
-----> Creating <default-centos65>...
...
Starting Chef Client, version 11.14.2
[2014-08-17T23:06:15-07:00] INFO: *** Chef 11.14.2 ***
[2014-08-17T23:06:15-07:00] INFO: Chef-client pid: 2386
[2014-08-17T23:06:16-07:00] INFO: Setting the run_list to ["recipe[chef
-server::default]"] from CLI options
[2014-08-17T23:06:16-07:00] INFO: Run List is [recipe[chef-server
::default]]
[2014-08-17T23:06:16-07:00] INFO: Run List expands to [chef-server
::default]
[2014-08-17T23:06:16-07:00] INFO: Starting Chef Run for default-centos65
[2014-08-17T23:06:16-07:00] INFO: Running start handlers
[2014-08-17T23:06:16-07:00] INFO: Start handlers complete.
Compiling Cookbooks...
Converging 3 resources
...
[2014-08-17T23:10:39-07:00] INFO: Chef Run complete in 262.973873452
seconds