chef-client not getting ENV variable value

443 views Asked by At

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?

1

There are 1 answers

0
i5z On

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 ...