Using search to find nodes
If you are running your infrastructure in any type of a virtualized environment, such as a public or private cloud, the server instances that you use will change frequently. Instead of having a well-known set of servers, you destroy and create virtual servers regularly.
Your cookbooks cannot rely on hardcoded server names when you need in this situation, a list of available servers.
Chef provides a way to find nodes by their attributes, for example, their roles. In this section, we'll see how you can retrieve a set of nodes to use them in your recipes.
Getting ready
Make sure that you have a cookbook called my_cookbook
, as described in the Creating and using cookbooks section in Chapter 1, Chef Infrastructure.
How to do it...
Let's see how we can find all nodes having a certain role:
Create a role called
web
that hasmy_cookbook
in its run list. This command will open a JSON definition of your role in your default editor. You need to add"recipe[my_cookbook]"
to"run_list...