Bjorn
Bjorn

Reputation: 71850

How can I lazily set an environment variable using env in Chef?

I put credentials into an s3 bucket and I use a ruby block to grab them. I then want to set an environment variable such that when upstart starts a process it uses this variable. However the block of ruby runs after attributes are set so I thought using lazy would be appropriate, but it's not clear to me how to set env using lazy.

Would it be something like:

ruby_block "get-credentials" do
  block do
    Chef::Log.info 'Getting sdk.'
    require 'aws-sdk'

    Chef::Log.info 'Getting making aws s3 instance.'
    s3 = AWS::S3.new


    Chef::Log.info 'Getting credentials from s3.'
    bar = s3.buckets['bucket-name'].objects['bar'].read
    Chef::Log.info 'Got bar with length #{bar.length}'

    node.set['foo']['bar'] = bar

  end
  action :run
end

env lazy BAR=node.set['foo']['bar']

service 'foo' do
    provider Chef::Provider::Service::Upstart
    action [ :enable, :start ]
end

I'm not sure. I am still looking through the documentation and experimenting but maybe someone knows. The turn around on testing different variations is taking a really long time.

Upvotes: 1

Views: 342

Answers (1)

coderanger
coderanger

Reputation: 54211

The env resource only works on Windows, it has nothing to do with Linux. If you want to define environment variables for an upstart service it has to go in the upstart config, as the environment within Chef has no effect on things spawned from upstart.

Upvotes: 3

Related Questions