Chef : How to add multiple nodes to a given role
chef-infra, chef-recipe
Solution
There is afaik no built-in API endpoint or `knife` subcommand to add a role to multiple nodes.
However, you can add do this this using knife exec:
knife exec -E 'nodes.find("chef_environment:dev") {|n| puts n.run_list << "role[base]" unless n.run_list.include?("role[base]"); n.save }'
The example filters by the environment `dev` and adds the `base` role. You can also filter for a certain node name (using `name:*`)
(example taken from dougireton.com)
Problem
Am a newbie to chef and trying out various options. If a use case is to add multiple nodes to a particular role what is the easiest way to do it? For a single node, I would execute the following command from the chefdk (Work station). `knife node run_list add <host-name1> "role[httpd-role]"` (Have already created and uploaded recipes to chef-server and combined a few recipes to create the role 'httpd-role'). Assuming, I have many hosts names what is the easiest way to add the above role to all the nodes in the following scenario. - When the host names have a fixed pattern with incremental numbers(fe1, fe2 etc...) - When the host names doesn't have any fixed pattern(foo, bar, fooabcd, barabcd etc...) At the end of this exercise, I would like to add the role 'httpd-role' to all the nodes and upon executing 'sudo chef-client' on all the nodes, I would expect to fetch all the latest policies and install them in the nodes. Thanks In Advance