Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chef : How to add multiple nodes to a given role

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.

  1. When the host names have a fixed pattern with incremental numbers(fe1, fe2 etc...)
  2. 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

like image 480
Pranesh Vittal Avatar asked Feb 17 '15 06:02

Pranesh Vittal


1 Answers

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)

like image 93
StephenKing Avatar answered Sep 25 '22 23:09

StephenKing