Reputation: 39
I am looking at the option of creating a LWRP for some common function we are using in our cookbooks. Is there a way to pass a node attribute and update it in LWRP?
The second option is to create library functions. If a node attribute is passed by the caller to the library function, how to go about setting a new value in the function?
Any pointers to these are much appreciated.
Upvotes: 1
Views: 867
Reputation: 4223
Really need more details to answer with much certainty.
Then you'll want to use a library function rather than a resource/provider. You don't have to "pass" anything, as the node
object is already available inside both libraries and LWRPs. You can access the attributes the same way you always do with node['path']['to']['attribute']
and set them the same as in a recipe with node.default['path']['to']['attribute']
(or .normal
, .override
, etc). If you wanted to be really fancy, and make the method variable, you could do something like this.
*untested code (takes a path to an attribute, and sets that attribute to the return value of a block, passing the current value of the attribute into the block)
def check_and_set(*attr_path, &check)
current, parent = node
leaf = nil
current = attr_path.reduce do |current, branch|
current[branch]
parent = current
leaf = branch
value = check.yield(current)
parent[leaf] = value
end
If you're not just reading an setting, then you may need an LWRP. Everything stated above still applies. The node
object is still available and you can read from and write to it just as you would in a recipe.
Upvotes: 1