我正在使用在AWS Opsworks中运行的Chef 11.10.4。
我无法弄清楚如何将元素lyr
与下面第07-14行中的节点属性node['aws-tag'][#{lyr}]
结合起来。
我正在使用字符串替换作为伪代码来显示我正在尝试做的事情但显然在这种情况下不起作用。线02-06完美运行,我只是想让07-14线工作,我不知道该怎么做。
include_recipe "aws"
unless node['aws-tag']['tags'].empty? || node['aws-tag']['tags'].nil?
aws_resource_tag node['ec2']['instance_id'] do
tags(node['aws-tag']['tags'])
action :update
end
node['opsworks']['instance']['layers'].each do |lyr|
unless node['aws-tag'][#{lyr}].empty? || node['aws-tag'][#{lyr}].nil?
aws_resource_tag node['ec2']['instance_id'] do
tags(node['aws-tag'][#{lyr}])
action :update
end
end
end
end
以下是来自chef-shell的结果,显示了上述代码中引用的属性的值:
chef > node['opsworks']['instance']['layers']
=> ["mongodb"]
chef > node['aws-tag']
=> {"tags"=>{"application"=>"app1", "environment"=>"dev"}, "mongodb"=>{"service"=>"mongodb"}}
我正在修改chef-aws-tag食谱以使用node['aws-tag']['tags']
中的标记标记所有ec2实例,并且还标记传递给opsworks的自定义JSON中包含的每个ec2实例opsworks图层特定标记图层,在这种情况下是node['aws-tag']['mongodb']
或node['aws-tag']['LAYER']
的内容,一般来说。
如果我运行上面的代码,我得到一个配方编译错误:“意外的keyword_do_block,期待']'” - 请参阅下面的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)
答案 0 :(得分:1)
lyr已经是一个字符串,因此以下工作:
node['opsworks']['instance']['layers'].each do |lyr|
unless node['aws-tag'][lyr].empty? || node['aws-tag'][lyr].nil?
aws_resource_tag node['ec2']['instance_id'] do
tags(node['aws-tag'][lyr])
action :update
end
end
end
我在chef-shell中以交互方式对它进行了测试并且它没有用,但我想在运行时执行它有一些区别,因为它确实在那里工作。这是我的测试的输出在chef-shell中失败,我认为是同样的事情:
root@mongodb2:/opt/aws/opsworks/current/bin# ./chef-shell -j /var/lib/aws/opsworks/chef/2014-10-28-17-15-30-01.json
loading configuration: none (standalone session)
Session type: standalone
Loading.......done.
This is the chef-shell.
Chef Version: 11.10.4
http://www.opscode.com/chef
http://docs.opscode.com/
run `help' for help, `exit' or ^D to quit.
Ohai2u ubuntu@mongodb2.localdomain!
chef > lyr=node['opsworks']['instance']['layers']
=> ["mongodb"]
chef > lyr
=> ["mongodb"]
chef > node['aws-tag'][lyr]
=> nil
chef >