Update mcollective.init according to OSCI-855
[packages/precise/mcollective.git] / website / reference / integration / chef.md
1 ---
2 layout: default
3 title: Using With Chef
4 toc: false
5 ---
6 [FactsOpsCodeOhai]: http://projects.puppetlabs.com/projects/mcollective-plugins/wiki/FactsOhai
7
8 If you're a Chef user you are supported in both facts and classes filters.
9
10 ## Facts
11 There is a [community plugin to enable Ohai][FactsOpsCodeOhai] as a fact source.
12
13 Using this plugin Ohai facts will be converted from:
14
15 {% highlight javascript %}
16   "languages": {
17     "java": {
18       "runtime": {
19         "name": "OpenJDK  Runtime Environment",
20         "build": "1.6.0-b09"
21       },
22       "version": "1.6.0"
23     },
24 {% endhighlight %}
25
26 to:
27
28 {% highlight ruby %}
29  "languages.java.version"=>"1.6.0",
30  "languages.java.runtime.name"=>"OpenJDK  Runtime Environment",
31  "languages.java.runtime.build"=>"1.6.0-b09",
32 {% endhighlight %}
33
34 So you can use the flattened versions of the information provided by Ohai in filters, reports etc.
35
36 {% highlight console %}
37 % mco find --with-fact languages.java.version=1.6.0
38 {% endhighlight %}
39
40 ## Class Filters
41 Chef does not provide a list of roles and recipes that has been applied to a node, to use with MCollective you need to create such a list.
42
43 It's very easy with Chef to do this in a simple cookbook.  Put the following code in a cookbook and arrange for it to run *last* on your node.
44
45 This will create a list of all roles and recipes in _/var/tmp/chefnode.txt_ on each node for us to use:
46
47 {% highlight ruby %}
48 ruby_block "store node data locally" do
49   block do
50     state = File.open("/var/tmp/chefnode.txt", "w")
51
52     node.run_state[:seen_recipes].keys.each do |recipe|
53         state.puts("recipe.#{recipe}")
54     end
55
56     node.run_list.roles.each do |role|
57         state.puts("role.#{role}")
58     end
59
60     state.close
61   end
62 end
63 {% endhighlight %}
64
65 You should configure MCollective to use this file by putting the following in your _server.cfg_
66
67 {% highlight ini %}
68 classesfile = /var/tmp/chefnode.txt
69 {% endhighlight %}
70
71 You can now use your roles and recipe lists in filters:
72
73 {% highlight console %}
74 % mco find --with-class role.webserver --with-class /apache/
75 {% endhighlight %}