Information Technology Reference
In-Depth Information
Fixing the second issue with the execute resource is a little more involved. That's why you
should prefer built-in Chef resources over using the execute resource, because it's up to you
to make the execute resources idempotent.
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, and if so the resource
does nothing. (If you need to test the reverse of this logic, which is more typical on Win-
dows, there is also an only_if guard—take a look at http://bit.ly/common_functionality for
more information:
execute "chef-server-ctl reconfigure" ddo
not_if "rpm -q chef-server"
end
end
While 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 by adding an action :nothing attribute. Also, you'll want to move the
actual command line explicitly to the command attribute, so that 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 ex-
ecute resource like so:
# reconfigure the installation
execute 'reconfigure-chef-server' ddo
command 'chef-server-ctl reconfigure'
action :nothing
end
end
Search WWH ::




Custom Search