Home > other >  How can you use a Chef recipe to set an environment variable by reading from Shell script?
How can you use a Chef recipe to set an environment variable by reading from Shell script?

Time:10-23

I need to read the credentials from Shell script and set it as environmental variable.

Previous code in chef

ENV['password'] = '123'

Now I'm trying to use below code but it is not working.

bash 'set_password' do
  code <<-EOH
          password=$(sh /opt/get_password.sh root)
      EOH
  guard_interpreter :bash
  environment ({ 'password' => $password })
end unless node.attribute?('ec2')

CodePudding user response:

If you want the output (stdout) of the get_password.sh script into an environment variable, you can simply do:

ENV['password'] = `/opt/get_password.sh root`

Then this environment variable can be referenced in other places of the recipe.

  • Related