AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 640026
Accepted
Peter M
Peter M
Asked: 2014-10-28 10:28:32 +0800 CST2014-10-28 10:28:32 +0800 CST 2014-10-28 10:28:32 +0800 CST

Chef / Ruby:使用带有节点属性的“each do”块变量

  • 772

在 AWS Opsworks 中运行的 Chef 11.10.4 环境。

我不知道如何使用节点属性传递下面的“each do”块变量“lyr”的值node['aws-tag'][#{lyr}]?

下面的代码块适用于第 02-06 行,但不适用于第 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

以下是 chef-shell 的结果,显示了上面代码中引用的属性值:

厨师>节点['opsworks']['instance']['layers'] => [“mongodb”]

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

注意:我正在修改chef-aws-tag食谱以使用标签标记所有 ec2 实例,node['aws-tag']['tags']此外还标记每个 ec2 实例 opsworks 层特定标签,这些标签包含在按层传递给 opsworks 的自定义 json 中,在这种情况下node['aws-tag']['mongodb']or的内容node['aws-tag']['LAYER'],一般来说。

如果我运行上面的代码,我会得到一个配方编译错误:“unexpected keyword_do_block, expecting ']'”——请参阅下面的 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)
chef
  • 1 1 个回答
  • 2763 Views

1 个回答

  • Voted
  1. Best Answer
    vgoff
    2014-10-29T10:24:25+08:002014-10-29T10:24:25+08:00

    第 8 行对我来说看起来很可疑,应该会引发错误,如所示......

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

    应该是字符串插值,但不是。

    试试这条线:

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

    如果lyr已经是一个字符串,那么只需lyr像这样使用:

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

相关问题

  • 如何开始使用 Chef?

  • 调用 chef-solo 的惯用方式?

  • centos 5.3上的厨师错误

  • 哪种方式是使用 Chef 安装软件包的最“优雅”方式?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve