Resource attribute environment


#1

Hello,

i have found several variants of the environment attribute.

environment “PATH” => "/my/path/to/bin:#{ENV[“PATH”]}"
environment ({‘HOME’ => ‘/home/myhome’})
environment { ‘VAR’ => ‘whatever’ }

What is the correct one and what is the difference?

Best Regards
Gordon


#2

On 2/16/15 2:17 AM, Gordon Franke wrote:

Hello,

i have found several variants of the environment attribute.

environment “PATH” => "/my/path/to/bin:#{ENV[“PATH”]}"
environment ({‘HOME’ => ‘/home/myhome’})
environment { ‘VAR’ => ‘whatever’ }

What is the correct one and what is the difference?

Best Regards
Gordon
Fairly certain the last one is incorrect, and the ruby parser interprets
environment { whatever } to mean that you’re passing a block rather
than a hash, and when it sees hashrockets (=>) in what it thinks is a
block it’ll throw a syntax error at you. This is a limitation of the
ruby language because {} can be ambiguous and mean either a hash or a
block.

I think these forms all work:

environment “foo” => “bar”, “fizz” => "buzz"
environment( “foo” => “bar”, “fizz” => “buzz” )
environment( { “foo” => “bar”, “fizz” => “buzz” } )

The choice between them comes down to style. I actually prefer the last
one in this specific case because its makes it the easiest to see that
it has to parse correctly, and is easiest on my aged braincells. I
think you’ll be forced into that case if you want to pass the empty hash
as well, and I don’t like rules with additional special cases:

environment # this is the accessor, does not set
environment() # this is the accessor, does not set
environment {} # passes a block
environment({}) # this correctly passes an empty hash