Why the chef recipe cannot read the values from bash environment variables

chefchef-solo

This is the content of my shell script:

export TARGET_ROLE=play
vagrant up

I am using the script to export environment variables in order to set some variables inside my chef cookbook

role = ENV['TARGET_ROLE']
Chef::Log.info("Using role #{role}")
node.override[:mainapp][:target_role] = role

seq = ENV['SEQ']
Chef::Log.info("Using seq #{seq}")
node.override[:mainapp][:server_seq] = seq 

Here is what I see in my log:

==> default: [2015-09-14T07:22:35+00:00] DEBUG: Found recipe mainapp_common_env in cookbook mainapp
==> default: [2015-09-14T07:22:35+00:00] INFO: Using role 
==> default: [2015-09-14T07:22:35+00:00] INFO: Using seq 

What have I done wrong?

Best Answer

You set an environment variable on the host. -- But Chef runs inside your Virtualbox VM.

To set environment variables for it use the binary_env option as described in the Vagrant documentation for Chef provisioning.

Related Topic