• Uncategorized

About linux : Environment-variables-not-getting-created-by-Chef

Question Detail

I am trying to create some new env variables in the rhel machine using chef.

The block executes successfully but on trying to echo the value, i am getting black result.

Script-1:

execute 'JAVA_HOME' do
      command 'export JAVA_HOME='+node['java']['home']
    end

Script-2:

bash 'env_test' do
      code <<-EOF
      echo $chef
    EOF
      environment ({ 'chef' => 'chef' })
    end

Also gave this a shot as it was mentioned in the documentation:

ENV['LIBRARY_PATH'] = node['my']['lib']

Please let me know where am i going wrong here..

Question Answer

So the thing you need to know about environment variables is they only work in one direction (parent process to children) so an export in a subcommand does nothing after that execute resource finishes. The second and third examples both work though, with the second setting it for just that bash resource and the third for both the Chef process and everything it spawns. Remember that you need to run with with -l debug to see the output from subcommands Chef runs.

Above explanation is pretty helpful. Updating the /etc/environments file using chef to make sure that env variables are present from the next session. Also using the 3rd approach to make the env variables available for the current session.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.