Syntax error, unexpected =>, expecting '}'

Hi,

Whats the deal with the online docs?

bash ‘javatooling’ do
environment {‘JAVA_HOME’ => ‘/usr/lib/java/jdk1.7/home’}
code 'java-based-daemon-ctl.sh -start’
not_if 'java-based-daemon-ctl.sh -test-started’end

And when I try and do it?

syntax error, unexpected =>, expecting '}'
environment {‘ES_HEAP_SIZE’ => ‘#{heap_size}gb’}

bash ‘ES_HEAP_SIZE’ do
code <<-EOH
touch /var/chef/cache/heap.lock
EOH
environment {‘ES_HEAP_SIZE’ => ‘#{heap_size}gb’}
action :run
not_if '/var/chef/cache/heap.lock’
end

Hi there – you have a < (less-than) instead of an opening curly brace:

bash 'ES_HEAP_SIZE' do
  code < '#{heap_size}gb'}
  action :run
  not_if '/var/chef/cache/heap.lock'
end

Should be:

bash 'ES_HEAP_SIZE' do
  code { "#{heap_size}gb" }
  action :run
  not_if '/var/chef/cache/heap.lock'
end

Hope this helps!

Martin

Hi David,

I think the problem lies with the Ruby syntax you're using, not the online docs.

As you pasted, from the docs:

This will evaluate the not_if, decide that the resource should run, and then execute the command in the code section, passing in the environment hash as variables to this exact command.

Your example:

So a few of things spring to mind:

  • The trailing brace that is triggering the error is the one right behind gb' in the code line.
  • I don't think using the < operator directly to a code string works
  • Trying to interpolate a string with single quotes will provide the exact string, not the value passed. Use double quotes for string interpolation:
[1] pry(main)> heap_size = 8
=> 8
[2] pry(main)> '#{heap_size}gb'
=> "\#{heap_size}gb"
[3] pry(main)> "#{heap_size}gb"
=> "8gb"
[4] pry(main)>

However, all this brings me to ask this:

It appears you are trying to set the environment variable for Elasticsearch heap size upon startup. If you're using a service block to do this, then you likely want to set the variable before the service startup?

Using a bash resource typically invokes a subprocess, passes along any environment settings and then exits, so those settings are not persisted for any other processes.

If so, there's a couple of ways to go about resolving.

  1. Add the value to your ES init script. See an example here.
  2. Set the environment variable before the service startup. See docs example here

Note that using option 2, the variable will only be present when starting the service via Chef - if you restart the service on command line, the environment variable will not be present.

There's a large variety of ways of setting environment variables in general, these are not the only ways to do it.

My preference is to have the value in the init script, then it's not in the environment, and can be inspected as a file later.

Hope this helps!
-M

The line environment {'JAVA_HOME' =>; '/usr/lib/java/jdk1.7/home'} is incorrect. What it’s trying to do is pass a Hash literal as an argument to a method call. But Ruby uses squiggly brackets for both Hash literals and code blocks (Procs), so to pass a Hash argument, you must either use parentheses around the argument or omit the squiggly brackets.

I’ll notify our documentation team about this.

You’re absolutely correct - I overlooked that. Using parentheses instead of curly braces works.

bash 'javatooling' do
  environment('JAVA_HOME' => '/usr/lib/java/jdk1.7/home')
  code 'java-based-daemon-ctl.sh -start'
  not_if 'java-based-daemon-ctl.sh -test-started'
end