How to query cookbook versions on a node?

2019-03-24 09:25发布

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?

7条回答
Bombasti
2楼-- · 2019-03-24 09:57

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
查看更多
一纸荒年 Trace。
3楼-- · 2019-03-24 10:00

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
查看更多
Rolldiameter
4楼-- · 2019-03-24 10:07

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.

查看更多
Ridiculous、
5楼-- · 2019-03-24 10:08

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

查看更多
唯我独甜
6楼-- · 2019-03-24 10:09

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
查看更多
一纸荒年 Trace。
7楼-- · 2019-03-24 10:16

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.

查看更多
登录 后发表回答