Search code examples
linuxbashenvironment-variableschef-infrachef-recipe

Prevent Chef from running the bash commands twice in Ubuntu 14.04


In Chef recipe, I use bash command to set up the env variables.

bash 'setup_env' do
code <<-EOH
    echo #{node['foo']} | sudo tee --append /etc/environment
    echo #{node['bar} | sudo tee --append /etc/environment
    for line in $( cat /etc/environment ) ; do export $line ; done
    EOH
end

This will write foo and bar into /etc/environment every time I run vagrant provision

The /etc/environment file ends up like this:

foo="foo"
bar="bar"
foo="foo"
bar="bar"

Is there a method to prevent this bash command from running multiples times in Chef?


Solution

  • The community cookbook etc_environment can be used to manage the "/etc/environment" file.

    Looking at its code you'll observe it uses a chef template to manage the file: