Optional resource additions at converge-time?


#1

I’m wondering if there is a better way to accomplish this. I have a
situation where I want to declare a resource optionally based on some
attributes and other factors. Simple enough right?

if criteria
package foo
end

or even

package foo do
only_if criteria
end

However the ruby if, and the only_if too it seems, are processed at
compile time so it will effectively ignore an attribute that is
set/changed in another cookbook/recipe that falls later in the
runlist. This is a common case if you have “base” cookbooks that
provide a bunch of settings with defaults that can be toggled as
needed by other more specific cookbooks. We worked around this by
using a ruby block to process the criteria and then optionally define
resources and shove them into run_contenxt.resource_collection but
it’d be great if there was a cleaner way. Are only_if/not_if meant to
apply at compile time? Is there / should there be a way to change
this? This also comes up with templates because the variables param
seems to be processed at compile time so if you pass in attributes
that may change during compilation of later recipes you can hit a
similar problem.

KC


#2

KC,

Try passing the condition as a block:

package “foo” do
only_if { condition }
end

Cheers,
Jay

On Mon, Jul 30, 2012 at 4:28 PM, KC Braunschweig
kcbraunschweig@gmail.comwrote:

I’m wondering if there is a better way to accomplish this. I have a
situation where I want to declare a resource optionally based on some
attributes and other factors. Simple enough right?

if criteria
package foo
end

or even

package foo do
only_if criteria
end

However the ruby if, and the only_if too it seems, are processed at
compile time so it will effectively ignore an attribute that is
set/changed in another cookbook/recipe that falls later in the
runlist. This is a common case if you have “base” cookbooks that
provide a bunch of settings with defaults that can be toggled as
needed by other more specific cookbooks. We worked around this by
using a ruby block to process the criteria and then optionally define
resources and shove them into run_contenxt.resource_collection but
it’d be great if there was a cleaner way. Are only_if/not_if meant to
apply at compile time? Is there / should there be a way to change
this? This also comes up with templates because the variables param
seems to be processed at compile time so if you pass in attributes
that may change during compilation of later recipes you can hit a
similar problem.

KC


#3

Found there’s this:

http://wiki.opscode.com/display/chef/Configuration+Based+on+Partial+Convergence

My use case isn’t actually partial-convergence but it amounts to the same issue.