Search code examples
chef-infrachef-recipeknifecookbook

How do i automate assigning nodes to environments?


You can assign nodes to environments when bootstrapping, but I already have a bunch of production nodes sitting in _default.

I could use knife to just assign them to other environments or I was thinking of having a role for each environment that has a recipe which runs chef-client::config and sets the environment attribute, then I could just assign nodes to the right environment role. The problem with using a role I think though would be the extra hoops I'd have to jump through to hook things up in test kitchen.

How do chef people usually solve this problem?


Solution

  • The right place to start is knife exec with a transform script.

    This should probably get you started:

    knife exec -E 'nodes.transform("roles:foo") {|n| n.chef_environment = "foo" }'
    

    The argument there is a search query (you can also use "*:*" to grab all the nodes) and then do something with each node object in the block. If the return value of the block is true-y it gets saved, if false-y it doesn't.