How to find the Chef environment in a recipe?
I want to run a cookbook_file resource only if the current environment is "dev". How can this be expressed?
The documentation suggests this:
In a recipe, a code block like this would be useful:
qa_nodes = search(:node,"chef_environment:QA") qa_nodes.each do |qa_node| # Do useful specific to qa nodes only end
But I'm not sure that's what I want - the fact it's a loop seems wrong.
Solution 1:
Look in the chef_environment Ruby attribute (not a regular Chef attribute) on the node:
if node.chef_environment == "dev"
# stuff
end
Solution 2:
another elegant way:
if ['production','development'].include? node.chef_environment
#do something here
end