Setting environment variables
You might have experienced this: you try out a command on your node's shell and it works perfectly. Now you try to execute the very same command from within your Chef recipe but it fails. One reason may be that certain environment variables set in your shell are not set during the Chef run. You might have set them manually or in your shell startup scripts – it does not matter. You'll need to set them again in your recipe.
In this section, you will see how to set environment variables during a Chef run.
Getting ready
Make sure you have a cookbook called my_cookbook
, and that the run_list
of your node includes my_cookbook
, as described in the Creating and using cookbooks recipe in Chapter 1, Chef Infrastructure.
How to do it…
Let's see how we can set environment variables from within Chef recipes:
Set an environment variable to be used during the Chef client run:
mma@laptop:~/chef-repo $ subl cookbooks/my_cookbook/recipes/default.rb ENV['MESSAGE'] = 'Hello from Chef...