Search code examples
chef-infra

chef only_if AND not_if in same resource


I'm confused about the logic order of only_if and not_if in Chef's resource model.

In the following example, assuming a file called /tmp/foo containing the word bar:

execute "rm /tmp/foo" do
    action :run
    only_if "ls /tmp/foo"
    not_if "grep bar /tmp/foo"
end

What happens? Are only_if and not_if exclusive? AND? OR? Which comes first? Why?


Solution

  • The are run in the order given in the code. The internals of each method basically boils down to @guards << Guard.new(*args) so they track order implicitly. Both clauses must "pass" for the action the run. In general this is a bad idea as it is super hard to read, if you must then leave a very very good comment.