How to query cookbook versions on a node?
Asked Answered
S

10

18

Usage case: The DevOps team launched a node sometime ago, and my team would like to know what's the version(s) of one/several cookbook(s) being used in the run_list. Our DevOps team is firefighting so we'd like to find a way to be self-sufficient.

Commands Tried: knife cookbook show COOKBOOK give all possible versions, but does not specify which one being used.

knife node show NODE shows all cookbooks, but there's no version info attached.

Question: Is there a command (something similar to knife search, ohai) to query the chef-server for the versions deployed on the node?

Schaal answered 22/6, 2015 at 18:51 Comment(1)
Common problem that has caused me no end of pain. See also #31013267Logistician
C
13

If you can ssh into the box you can look under /var/chef/cache/cookbooks/<cookbook name>/metadata.json to find the version.

Also, you can access it during a chef run by looking at @run_context.cookbook_collection, but that probably doesn't help.

Generally the cookbook version is defined by the environment, but since environments change over time, you can't really trust that to be the same set that was used when this node last converged (especially if it's been a while).

By far your safest option will be to look at the chef cache.

Camphene answered 22/6, 2015 at 20:21 Comment(3)
The only limitation: root access needed :( -rw------- 1 root root 590 Jul 20 22:00 metadata.rbSchaal
If you have jshon, can do find /var/chef/cache/cookbooks -type f -name 'metadata.json' -exec bash -c "cat {} | jshon -e name -u -p -e version -u | paste -s -d =" \; | sortGretna
I found it under metadata.rb, not JSON. Maybe that's a Chef 11 thing, though?Grillo
H
6

If you're using ohai (you probably are), you can do something like this:

knife search -i 'cookbooks:your-cookbook' -a cookbooks.your-cookbook.version

This will give you output that shows the hostname and the cookbook version:

1 items found

server.name.example:
  cookbooks.cs-redis.version: 0.3.2
Heterogamete answered 1/3, 2017 at 13:10 Comment(1)
Or you could simply query the node you're interested in like so: knife node show <NODE_NAME> -a cookbooksSeringapatam
J
5

In our organisation we use a base cookbook to set an attribute on the node with the cookbook versions.

run_context.cookbook_collection.each do |key, cookbook|
  node.set['base_cookbook']['cookbook_versions'][cookbook.name] = cookbook.version
end

Then we can query the versions used by a node with

knife node show <node-name> -a base_cookbook.cookbook_versions
Junco answered 25/6, 2015 at 9:11 Comment(4)
Just for completeness I'm wondering: Would the current version of this answer would retain cookbooks that no longer are in the run-list?Dyak
It turns out that there is a cookbook in the Supermarket that implements this same technique: supermarket.chef.io/cookbooks/cookbook_versionsDyak
I believe you can now do this directly without the run_context base cookbook. Perhaps built in to chef 12. knife node show <node-name> -a cookbooks works for me.Adest
Thanks Beel, How to get multiple attributes when I use knife node show nodename -a attr1, attrib2, etcInimical
N
1

Came across this post and ended up working out a grep command to do this.

sudo grep -o -e '\"version\"\:\"[a-zA-Z0-9.]*\"' -e '\"version\"\: \"[a-zA-Z0-9.]*\"' /var/chef/cache/cookbooks/*/metadata.json
Noria answered 30/8, 2016 at 14:9 Comment(0)
E
1

I had a similar requirement where I published a new version of cookbook and wanted to find which nodes were using the latest version of my cookbook. The below knife commands worked for me.

knife search -i node "cookbooks:<cookbook-name> AND cookbooks_<cookbook-name>_version:100.1.0"
Encounter answered 22/5, 2019 at 20:51 Comment(1)
Thank you for this! We had an issue where we need to backport an attribute override to older versions of an internal cookbook, this helped immensely when hunting down nodes that were pinned to an older version of the cookbook in scope.Cerberus
M
0

I can think of a two steps solution.

Step 1: knife node show <%node-name%>. The output should include the Environment being used on the node.

Step 2: knife environment show <%environment-name%>. This output should detail all the cookbooks being deployed on the node with their versions

Millepore answered 22/6, 2015 at 23:3 Comment(2)
This will hide you all the cookbooks that are included as dependencies.Unnecessary
Environments only work if you have explicitly setup cookbook constraints.Logistician
R
0

I'm unsure of a way via knife, but you can log into your Managed Chef at https://manage.chef.io and navigate to the nodes section for your organization. Click on the node name in question, and at the bottom right, under Run List, click the Expand All link. That will show you the versions of the cookbook each recipe is run as.

Richey answered 4/7, 2015 at 4:11 Comment(1)
This will show recipes, and recipes nested under roles, but will not expand to show dependencies.Gretna
I
0

I am using this (and versions of) for Windows clients

Invoke-Command -ComputerName $nodename -ScriptBlock { gci "c:\chef\cache\cookbooks\*\metadata.rb"  | % { select-string $_ -pattern '^version.*$' } | % { $_.Path.replace('\metadata.rb','') } } -Credential $creds
Intelligence answered 14/2, 2019 at 13:46 Comment(0)
P
0

This worked for me:

knife search node '*:*' -a 'cookbook_versions.<cookbook_name>'
Palacio answered 8/6, 2022 at 21:24 Comment(0)
B
0

Uses -a cookbooks, which works for me perfectly.

knife search node '<node_search-*>' -a cookbooks

<node-123.server.example.com>:
  cookbooks:
    <a_sample_item>:
      version: <7.5.17>
    <but_another-one>:
      version: <6.0.27>
...

Or eliminate other cookbooks by using -a cookbooks.<cookbook> as below:

knife search node "name:<node_search-*>" -a cookbooks.<cookbook>

<node-123.server.example.com>:
  cookbooks:
    <cookbook>:
    version: <7.5.17>
<node-456.server.example.com>:
  cookbooks:
    <cookbook>:
    version: <1.2.3>
Benzaldehyde answered 21/2 at 23:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.