meallhour
meallhour

Reputation: 15571

What is the best way to assign environment variable inside chef recipe?

I want to assign a system variable within chef recipe
I am using the following code:

env 'DEF_ADDR' do
  value "http://#{node['ipaddress']}"
end  

However, I am getting the below error on executing the recipe

ERROR: Cannot find a resource for env on redhat version 6.6

Upvotes: 0

Views: 1863

Answers (2)

coderanger
coderanger

Reputation: 54181

There is no consistent way to set global environment variables on Unix. Some distros support global-level shell includes via things like /etc/profile.d and the like, but this will have no effect on things run outside of a shell like direct SSH execution or running as a service.

Upvotes: 1

zuazo
zuazo

Reputation: 5738

The env resource seems to be only for Windows environments:

Use the env resource to manage environment keys in Microsoft Windows.

If you want to define an environment variable only for the Chef Run, you can use Ruby:

ENV['DEF_ADDR'] = "http://#{node['ipaddress']}"

But this will only be accessible during the Chef Run.

If you want to define a system-wide environment variable, maybe the etc_environment cookbook could help you with that:

node.default['etc_environment']['DEF_ADDR'] = "http://#{node['ipaddress']}"

Upvotes: 1

Related Questions