If you want to check whether a node run_list
includes a specific role (upon
expansion), then you could use role? method on the Node
object:
node.role?('name')
Alternatively, you can see whether either would work for you:
node.roles.include?('name')
node.run_list?('role[name]')
There exist also node.run_state[:seen_recipes]
, but it often has different use case.
Methods on the Node
object:
recipe?
https://github.com/opscode/chef/blob/master/lib/chef/node.rb#L247
role?
https://github.com/opscode/chef/blob/master/lib/chef/node.rb#L259
run_list?
https://github.com/opscode/chef/blob/master/lib/chef/node.rb#L288
All of the above should be present in reasonably up-to-date version of the Chef Client.
Let's see whether they work ... Details about the node:
[chef@C3394864441 ~]$ knife node show ChefDK
Node Name: ChefDK
Environment: _default
FQDN: C3394864441.domain
IP: 10.160.200.69
Run List: role[base_workstation]
Roles: base_workstation
Recipes: workstation, workstation::default
Platform: centos 6.5
Tags:
Details about the role:
[chef@C3394864441 ~]$ knife role show base_workstation
chef_type: role
default_attributes:
chef_client:
interval: 60
splay: 30
push_jobs:
package_url: https://s3.amazonaws.com/opscode-private-chef/el/6/x86_64/opscode-push-jobs-client-1.0.2-1.el6.x86_64.rpm
description: Base role for workstation nodes
env_run_lists:
json_class: Chef::Role
name: base_workstation
override_attributes:
run_list: recipe[workstation]
[chef@C3394864441 ~]$
Testing to see what is included in the run_list (running chef-shell
in client mode):
[chef@C3394864441 ~]$ sudo chef-shell -z
loading configuration: /etc/chef/client.rb
Session type: client
Loading....resolving cookbooks for run list: ["workstation"]
Synchronizing Cookbooks:
- workstation
.done.
This is the chef-shell.
Chef Version: 11.14.0.rc.2
http://www.opscode.com/chef
http://docs.opscode.com/
run `help' for help, `exit' or ^D to quit.
Ohai2u [email protected]!
chef > node.recipe?('workstation')
=> true
chef > node.recipe?('base_workstation')
=> false
chef > node.role?('workstation')
=> false
chef > node.role?('base_workstation')
=> true
chef > node.run_list?('workstation')
=> false
chef > node.run_list?('base_workstation')
=> false
chef > node.run_list?('recipe[workstation]')
=> false
chef > node.run_list?('role[workstation]')
=> false
chef > node.run_list?('role[base_workstation]')
=> true
chef >
We can see that all methods work, but only recipe?
looks for given recipes
in the expanded run_list
. For instance, base_workstation
will expand into
workstation at the beginning of the Chef client run, so all the recipes it
includes will be applied accordingly and in order during the node convergence.
Other ideas and/or explanation can be found here: http://stackoverflow.com/questions/22865769/how-to-check-if-recipe-has-been-included-using-include-recipe