Information Technology Reference
In-Depth Information
One way to fix this issue is with a not_if guard to the execute resource. Guards are used to
make a resource idempotent by allowing the resource to test for a desired state, and if the de-
sired state is present, the resource should do nothing. In this case, we'll test to see if the chef-
server package is already installed, by adding a not_if clause to the execute resource as
follows. not_if will test to see if the exit code of the command is 0; if so, the resource does
nothing.
NOTE
If you need to test the opposite logic of not_if , there is also an only_if guard. It's more
typical to use only_if on Windows, given that a successful exit code for Windows com-
mands is frequently the value 1 instead of 0. Take a look at http://bit.ly/com-
mon_functionality for more information.
execute "chef-server-ctl reconfigure" ddo
not_if "rpm -q chef-server"
end
end
Although this is a reasonable way to address the issue, it's a little clunky. You have to figure
out a way to detect whether Chef Server is installed, and the method used in the previous ex-
ample is not very reliable. A better approach is to trigger the execute when the package re-
source installs the package. You can trigger events in other resources with a notifies state-
ment.
In order to use notifies , we'll need to change the execute resource statement a bit. First,
you'll want to change the resource so it does nothing by default when execute is evaluated
during the Chef run; we do this by adding an action :nothing attribute. Also, you'll want
to move the actual command line explicitly to the command attribute, so you can use a short
name to trigger the execute block. By default, the name passed to the execute resource as a
string parameter is used as the command attribute, which is great for a self-documenting one-
liner. but not so great when you want to trigger the command by name. So let's transform the
execute resource like so:
# reconfigure the installation
execute 'reconfigure-chef-server' ddo
command 'chef-server-ctl reconfigure'
Search WWH ::




Custom Search