How to validate attribute before recipe is being executed?

For some attributes, our use case is it must be overrided by user, and
it need to follow our validation scheme, e.g. must be an integer <
100, is it possible to validate it before the recipe is being
executed?

On Wed, Mar 13, 2013 at 8:18 AM, howard chen howachen@gmail.com wrote:

For some attributes, our use case is it must be overrided by user, and
it need to follow our validation scheme, e.g. must be an integer <
100, is it possible to validate it before the recipe is being
executed?

Where/how is the user going to enter the data to override the attribute? If
I was to implement this[a], I would
pick one of two strategies.

  1. Put data in local file on the node, write an ohai plugin to read the
    file and validate the attribute.

  2. Put the data in a databag, and have some gateway process validate the
    entry before it gets put in
    the databag.

  • Booker C. Bense

[a]- I have not implemented it yet, but it's definitely on the to do
list...

Howard,

You can do something like this in your recipe:

my_value = node["my-cookbook"]["my-namespace"]["my-key"]

unless my_value.kind_of?(Integer) && (1..100).include?(my_value)
  Chef::Application.fatal! "Fix your value! It was: #{my_value.inspect}"
end

This will be executed after the attributes from all roles are applied.

This will be executed before any resources from any recipes in the run-list
are converged (i.e., before the system is altered).

Cheers,
Jay

On Wed, Mar 13, 2013 at 11:18 AM, howard chen howachen@gmail.com wrote:

For some attributes, our use case is it must be overrided by user, and
it need to follow our validation scheme, e.g. must be an integer <
100, is it possible to validate it before the recipe is being
executed?