I am looking for some ideas/guidance to use environments in chef to specify attributes that are specific to each environments like Dev, QA & PROD.

As a result we would like to be able to use same cookbooks across all environments but pass some environment specific variables via these attributes.

Has anyone come across this scenario. Thanks!


Here is a how we can use same cookbook across multiple environments but pass different variables specific to environment!

1. create an env.rb with below content and upload it via knife command.

name "dev"
description "This is for Development Environment"
cookbook "cookbook1", "= 0.1.0"
default_attributes "dev" => { "proxy-server" => "proxydev.internal.com" }

2. Write a recipe that calls the attribute to pass it as variable:

ruby_block "configure proxy" do 
  block do
   file = Chef::Util::FileEdit.new "/tmp/proxy/server"
   file.insert_line_if_no_match "^proxy", "proxy: #{node["#{dev}"]["proxy-server"]}"

3. Then bootstrap your node with this "dev" environment and include the above recipe in the runlist.

Result: Finally on the node, the file "/tmp/proxy/server" should contain an entry like below:

proxy: proxydev.internal.com

This worked for us!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.