Search code examples
rubybashgitchef-infracookbook

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


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')

Solution

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