1

Chef 11.10.4 environment running in AWS Opsworks.

I can't figure out how to pass the value of the "each do" block variable, "lyr," below, with the node attribute node['aws-tag'][#{lyr}]?

The code block below works from lines 02-06 but NOT from lines 07-14.

[01] include_recipe "aws"
[02] unless node['aws-tag']['tags'].empty? || node['aws-tag']['tags'].nil?
[03]    aws_resource_tag node['ec2']['instance_id'] do
[04]        tags(node['aws-tag']['tags'])
[05]        action :update
[06]    end
[07]    node['opsworks']['instance']['layers'].each do |lyr|
[08]        unless node['aws-tag'][#{lyr}].empty? || node['aws-tag'][#{lyr}].nil?
[09]            aws_resource_tag node['ec2']['instance_id'] do
[10]                tags(node['aws-tag'][#{lyr}])
[11]                action :update
[12]            end
[13]        end
[14]    end
[15] end

Here are the results from chef-shell showing the values of the attributes referenced in the code above:

chef > node['opsworks']['instance']['layers'] => ["mongodb"]

chef > node['aws-tag'] => {"tags"=>{"application"=>"app1", "environment"=>"dev"}, "mongodb"=>{"service"=>"mongodb"}}

NOTE: I'm modifying the chef-aws-tag cookbook to tag all ec2 instances with the tags in node['aws-tag']['tags'] and in addition to tag each ec2 instance opsworks layer specific tags that are included in custom json passed to opsworks by layer, in this case the contents of node['aws-tag']['mongodb'] or node['aws-tag']['LAYER'], generically.

If I run the code above I get a recipe compile error: "unexpected keyword_do_block, expecting ']'" -- see below for the detailed error message from opsworks:

================================================================================
Recipe Compile Error in /var/lib/aws/opsworks/cache.stage2/cookbooks/chef-aws-tag/recipes/ec2.rb
================================================================================


SyntaxError
-----------
/var/lib/aws/opsworks/cache.stage2/cookbooks/chef-aws-tag/recipes/ec2.rb:9: syntax error, unexpected keyword_do_block, expecting ']'
/var/lib/aws/opsworks/cache.stage2/cookbooks/chef-aws-tag/recipes/ec2.rb:12: syntax error, unexpected keyword_end, expecting ']'


Cookbook Trace:
---------------
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/mixin/from_file.rb:30:in `instance_eval'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/mixin/from_file.rb:30:in `from_file'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/cookbook_version.rb:237:in `load_recipe'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/run_context.rb:151:in `load_recipe'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/run_context/cookbook_compiler.rb:139:in `block in compile_recipes'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/run_context/cookbook_compiler.rb:137:in `each'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/run_context/cookbook_compiler.rb:137:in `compile_recipes'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/run_context/cookbook_compiler.rb:74:in `compile'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/run_context.rb:86:in `load'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/policy_builder/expand_node_object.rb:75:in `setup_run_context'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/client.rb:252:in `setup_run_context'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/client.rb:413:in `do_run'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/client.rb:200:in `block in run'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/client.rb:194:in `fork'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/client.rb:194:in `run'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/application.rb:215:in `run_chef_client'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/application/client.rb:314:in `block in run_application'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/application/client.rb:306:in `loop'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/application/client.rb:306:in `run_application'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/application.rb:66:in `run'
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/bin/chef-client:26:in `<top (required)>'
/opt/aws/opsworks/current/bin/chef-client:16:in `load'
/opt/aws/opsworks/current/bin/chef-client:16:in `<main>'


Relevant File Content:
----------------------
/opt/aws/opsworks/releases/20141010064749_328/vendor/bundle/ruby/2.0.0/gems/chef-11.10.4/lib/chef/mixin/from_file.rb:

23: 
24:        # Loads a given ruby file, and runs instance_eval against it in the context of the current
25:        # object.
26:        #
27:        # Raises an IOError if the file cannot be found, or is not readable.
28:        def from_file(filename)
29:          if File.exists?(filename) && File.readable?(filename)
30>>           self.instance_eval(IO.read(filename), filename, 1)
31:          else
32:            raise IOError, "Cannot open or read #{filename}!"
33:          end
34:        end
35: 
36:        # Loads a given ruby file, and runs class_eval against it in the context of the current
37:        # object.
38:        #
39:        # Raises an IOError if the file cannot be found, or is not readable.



[2014-10-28T17:43:22+00:00] ERROR: Running exception handlers
[2014-10-28T17:43:22+00:00] ERROR: Exception handlers complete
[2014-10-28T17:43:22+00:00] FATAL: Stacktrace dumped to /var/lib/aws/opsworks/cache.stage2/chef-stacktrace.out
[2014-10-28T17:43:22+00:00] ERROR: /var/lib/aws/opsworks/cache.stage2/cookbooks/chef-aws-tag/recipes/ec2.rb:9: syntax error, unexpected keyword_do_block, expecting ']'
/var/lib/aws/opsworks/cache.stage2/cookbooks/chef-aws-tag/recipes/ec2.rb:12: syntax error, unexpected keyword_end, expecting ']'
[2014-10-28T17:43:22+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
  • #{variable} is used for string interpolation, and it is not in quotes. I am not running the code, but I am surprised it is not raising errors for you as the rest of that line from the octothorpe on should be considered a comment. – vgoff Oct 27 '14 at 22:26
  • @vgoff -- I understand that I am using a string interpolation. I am new to ruby/chef and don't know how to do it with a non string which is why I posted the question. I attached the error output to the original question. – Peter M Oct 28 '14 at 18:02
  • Ah, but you aren't using String Interpolation, though it appears you want to. Wrote an answer. – vgoff Oct 28 '14 at 18:24
1

Line 8 looks suspect to me, should raise errors, as indicated...

unless node['aws-tag'][#{lyr}].empty? || node['aws-tag'][#{lyr}].nil?

Should be string interpolation, but it isn't.

Try this line:

unless node['aws-tag']["#{lyr}"].empty? || node['aws-tag']["#{lyr}"].nil?

If lyr is already a string, then simply use lyr like so:

unless node['aws-tag'][lyr].empty? || node['aws-tag'][lyr].nil?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.