So I’ve been trying to write a script which will show me which cookbooks
are used in an environment that are not version-locked.
I thought I have it all figured out, with the following command fetching a
list of cookbooks used in an environment. It collects “recipes” attribute
of all nodes in the environment and extracts cookbook names:
knife search node "chef_environment:{CHEF_ENVIRONMENT}" -a recipes| grep
-v -e ^id -e ^recipes -e found -e ^$ | awk -F’:’ ‘{print $1}’ | sed ‘s/^
*//; s/; */;/g’ |sort|uniq
The command does its job, but it has a major downside: it doesn’t list
cookbooks that are not explicitly listed in the “recipes” attribute. So,
for example, if “tomcat” cookbook calls “java” cookbook, and "tomcat"
cookbook is assigned to some node in the environment, the command above
will list “tomcat” but not “java”.
Is there a better way to fetch a list of cookbooks? Or should I add logic
to resolve cookbook dependencies? Thanks.
So I've been trying to write a script which will show me which cookbooks
are used in an environment that are not version-locked.
I thought I have it all figured out, with the following command fetching a
list of cookbooks used in an environment. It collects "recipes" attribute
of all nodes in the environment and extracts cookbook names:
The command does its job, but it has a major downside: it doesn't list
cookbooks that are not explicitly listed in the "recipes" attribute. So,
for example, if "tomcat" cookbook calls "java" cookbook, and "tomcat"
cookbook is assigned to some node in the environment, the command above
will list "tomcat" but not "java".
Is there a better way to fetch a list of cookbooks? Or should I add logic
to resolve cookbook dependencies? Thanks.
Thanks I gave it a try and so far after correcting syntax errors it's
saying that there's no recipes attribute for the array (n, I guess), so
I'll have to look into that.
So I've been trying to write a script which will show me which cookbooks
are used in an environment that are not version-locked.
I thought I have it all figured out, with the following command fetching
a list of cookbooks used in an environment. It collects "recipes" attribute
of all nodes in the environment and extracts cookbook names:
The command does its job, but it has a major downside: it doesn't list
cookbooks that are not explicitly listed in the "recipes" attribute. So,
for example, if "tomcat" cookbook calls "java" cookbook, and "tomcat"
cookbook is assigned to some node in the environment, the command above
will list "tomcat" but not "java".
Is there a better way to fetch a list of cookbooks? Or should I add logic
to resolve cookbook dependencies? Thanks.
Thanks I gave it a try and so far after correcting syntax errors it's
saying that there's no recipes attribute for the array (n, I guess), so
I'll have to look into that.
So I've been trying to write a script which will show me which cookbooks
are used in an environment that are not version-locked.
I thought I have it all figured out, with the following command fetching
a list of cookbooks used in an environment. It collects "recipes" attribute
of all nodes in the environment and extracts cookbook names:
The command does its job, but it has a major downside: it doesn't list
cookbooks that are not explicitly listed in the "recipes" attribute. So,
for example, if "tomcat" cookbook calls "java" cookbook, and "tomcat"
cookbook is assigned to some node in the environment, the command above
will list "tomcat" but not "java".
Is there a better way to fetch a list of cookbooks? Or should I add
logic to resolve cookbook dependencies? Thanks.
i think the bug was in the search call, which returns an array with 3
element, first being the nodes, second is starting index and third with
total nodes,
Thanks I gave it a try and so far after correcting syntax errors it's
saying that there's no recipes attribute for the array (n, I guess), so
I'll have to look into that.
So I've been trying to write a script which will show me which
cookbooks are used in an environment that are not version-locked.
I thought I have it all figured out, with the following command
fetching a list of cookbooks used in an environment. It collects "recipes"
attribute of all nodes in the environment and extracts cookbook names:
The command does its job, but it has a major downside: it doesn't list
cookbooks that are not explicitly listed in the "recipes" attribute. So,
for example, if "tomcat" cookbook calls "java" cookbook, and "tomcat"
cookbook is assigned to some node in the environment, the command above
will list "tomcat" but not "java".
Is there a better way to fetch a list of cookbooks? Or should I add
logic to resolve cookbook dependencies? Thanks.
Thanks a lot, this works. I've added some formatting and gotten rid of
"ERROR: ArgumentError: Attribute recipes is not defined!" error by adding
an extra check. The error showed up because we have some node objects that
don't have a runlist.
require 'chef'
Chef::Config.from_file('knife.rb')
envs = Chef::Environment.list.keys
q = Chef::Search::Query.new
envs.sort.each do |e|
if e.include? "sandbox"
next
end
puts "Environment: #{e}\n"
constrained_cookbooks = Chef::Environment.load(e).cookbook_versions.keys
all_cookbooks =
nodes = q.search(:node, "chef_environment:#{e}").first.reject(&:nil?)
nodes.each do |n|
if !n or !n.has_key? 'recipes'
next
end
n.recipes.map{|r| r.split('::')[0]}.each do |cookbook|
all_cookbooks << cookbook unless all_cookbooks.include?(cookbook)
end
end
puts "Unconstrained cookbooks: #{(all_cookbooks -
constrained_cookbooks).join ' '}\n\n"
end
i think the bug was in the search call, which returns an array with 3
element, first being the nodes, second is starting index and third with
total nodes,
Thanks I gave it a try and so far after correcting syntax errors it's
saying that there's no recipes attribute for the array (n, I guess), so
I'll have to look into that.
So I've been trying to write a script which will show me which
cookbooks are used in an environment that are not version-locked.
I thought I have it all figured out, with the following command
fetching a list of cookbooks used in an environment. It collects "recipes"
attribute of all nodes in the environment and extracts cookbook names:
The command does its job, but it has a major downside: it doesn't list
cookbooks that are not explicitly listed in the "recipes" attribute. So,
for example, if "tomcat" cookbook calls "java" cookbook, and "tomcat"
cookbook is assigned to some node in the environment, the command above
will list "tomcat" but not "java".
Is there a better way to fetch a list of cookbooks? Or should I add
logic to resolve cookbook dependencies? Thanks.