Dynamic Role Attributes in Chef

Dynamic Role Attributes in Chef

If you add normal attributes via knife bootstrap -j …, and leave override attributes in the role, the override will take over (see http://docs.opscode.com/essentials_node_object_attributes_precedence.html for a complete list of attribute precedence). If you have deleted override_attributes from db_role.rb before running knife bootstrap, or changed it to default_attributes, then setting IP in node attributes should have worked.

The last snippet won't work: roles are static JSON documents on the Chef server, and Ruby is only interpreted once by knife when uploading role to the server (http://docs.opscode.com/essentials_roles_formats.html). You can't refer to node's attributes from role's Ruby code, as it's compiled to JSON before it even touches any node. If you want to try a similar approach, you need to use a custom cookbook (say, my_network_interfaces) with a recipe that would look somewhat like this:

node.set['network_interface']['device'] = …
node.set['network_interface']['address'] = …
include_recipe 'network_interfaces'

This way, you'd use network_interfaces as a "library" cookbook, called by your "application" cookbook my_network_interfaces which implements whatever logic you need. From your question, I can't suggest how would you compute device and address, as your example just tries to copy same attributes, which is a no-op. As far as I understand what you want to achieve, you want to have default_attributes in the role, and pass specific JSON with normal attributes to knife bootstrap to override the defaults.

Dynamically Populate Chef Cookbook Attributes

i might be wrong, but it seems you are trying to invent the wheel. let me elaborate...

vagrant

from vagrant perspective, you should leverage test-kitchen in conjunection with vagrant driver.

once you set it up, take advantage of custom kitchen configuration -- use general configuration and (user) custom configuration (each user might have a different custom conifugration, corresponding to the config.yml you mentioned`)

note also, that kitchen.yml supports erb. you can use it to do things dynamically.

chef

from chef perspective, is a configuration management and you should use it correctly. that is, rather than using an external file (config.yml) to feed chef with values, take advantage of recipe attributes, json attributes or even data bags to store values which will be used during your recipe.

note that attributes can be generated dynamically, you will just have to put some logic for that (same as you did for config.yml)



Related Topics



Leave a reply



Submit