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 withvagrant
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
Hw Impossibility: "Create a Rock Paper Scissors Program in Ruby Without Using Conditionals"
Ruby on Rails Private Link Sharing: Google Docs Style
Ubuntu 10 Ruby 1.9 Rails 3: No Such File or Directory
Use a String to Access a Local Variable by Name
How to Troubleshoot Memory Bloat at Boot for Rails App
Multi Step Form with Image Uploader
How to Handle 404 Errors with Ruby Http::Net
How Can Multiple Trailing Slashes Can Be Removed from a Url in Ruby
Typeerror: Can't Convert Net::Httpok into String
How to Calculate a String's Width in Ruby
Binding to Networking Interfaces in Ruby
Running "Bundle Install" Fails and Asks Me to Run "Bundle Install"
Is 'Respond_To_Missing''s Second Argument Useful for Anything
Rails Asset Pipeline and Digest Values
Use Pry in Gems Without Modifying The Gemfile or Using 'Require'
Ruby Interpreter (Cui) 1.9.2P180 [I386-Mingw32] Has Stopped Working (I Am Not Using MySQL )