0

I am trying to read environment variables in recipes.

# recipes/default.rb

begin
  tenant_name = ENV['TENANT_NAME']
  ...
end

But tenant_name is always empty. I also tried to set default attributes but still empty value. I know there is -j option for json input data, but since it persist data on chef server, we do not want to use that option (that breaks other CI/CD flow).

How can I fix this? Why ENV variable is empty?

On the host where I run chef-client command has lot of ENV variables, but printing ENV.keys inside recipe shows only few of them. Does chef has allowlist/whitelist for what ENVs it imports by default?

i5z
  • 15
  • 6
  • 3
    Chef runs as a root user. Make sure you assign env variable for the user Chef runs as. – Draco Ater Oct 14 '20 at 08:02
  • Thank you @DracoAter for the hint. I was able to fix the issue by adding env variable to chef-client command. – i5z Oct 14 '20 at 20:44

1 Answers1

0

As @DracoAter suggested, the issue is really how command is being run. Since chef-client runs as root, the environment variable set by non-root user on the host was not visible to the chef-client command.

As a fix running command this way overcomes the problem,

sudo TENANT_NAME=tnt1 chef-client -o tenant_deploy ...
i5z
  • 15
  • 6