Re: Optional resource additions at converge-time?

Hi KC,

Just joined the mailing list, found this discussion in the archive, which is the same problem that I’ve just encountered. It is concerned with the conditional addition of resources at converge-time, based on checks carried out during converge-time. Putting the check in a ruby block, storing the result in a variable and using this variable as the only_if/not_if condition in subsequent resources seems to work for me - see the pattern at the end of this mail.

In the example below, assuming that is_ready? function returns true, TestResource1 is skipped and TestResource2 is executed. In my scenario, the result of is_ready? is affected by actions carried earlier in this chef run. I also compared the use of ruby blocks vs. strings. In my tests, they both appear to be executed at converge time, which I think is different to what you experienced? In the example below, TestResource3 is skipped, but TestResource4 is executed because the prior directory resource has made the only_if condition to be true. TestResource5 is also skipped.

Hope this is of use.

Is there a better way to do this? I’d prefer to have all the resources nested under a single check of the condition, instead of using multiple only_if/not_if statements. I tried nesting resources under a ruby block, but I couldn’t see how to do it; could be my ignorance of correct syntax.

Thanks,
Tom

ruby_block “Pacemaker cluster configuration check” do
block do
$ready =is_ready?()
end
end

Testing use of ruby block in condition

execute “TestResource1” do
not_if {$ready}
command "echo Tomtest1"
action :run
end

execute " TestResource2" do
only_if {$ready}
command "echo Tomtest2"
action :run
end

Testing use of string in condition

directory “/tmp/tomtestdir” do
owner "root"
group "root"
action :delete
end

execute " TestResource3" do
only_if "test -e /tmp/tomtestdir"
command "echo Tomtest3"
action :run
end

directory “/tmp/tomtestdir” do
owner "root"
group "root"
action :create
end

execute " TestResource4" do
only_if "test -e /tmp/tomtestdir"
command "echo Tomtest4"
action :run
end

execute " TestResource5" do
not_if "test -e /tmp/tomtestdir"
command "echo Tomtest5"
action :run
end