【问题标题】:dynamic usage of attribute in recipe配方中属性的动态使用
【发布时间】:2015-04-20 13:31:01
【问题描述】:

我正在尝试在配方中动态增加值并在另一个资源中使用,但仍然没有这样做。

Chef::Log.info("I am in #{cookbook_name}::#{recipe_name} and current disk count #{node[:oracle][:asm][:test]}") 

bash "beforeTest" do

  code lazy{ echo #{node[:oracle][:asm][:test]} }

end

ruby_block "test current disk count" do
  block do
    node.set[:oracle][:asm][:test] = "#{node[:oracle][:asm][:test]}".to_i+1
  end
end

bash "test" do
  code lazy{ echo #{node[:oracle][:asm][:test]} }
end

但是我仍然收到以下错误:

NoMethodError ------------- undefined method echo' for Chef::Resource::Bash 

Cookbook Trace: --------------- 
/var/chef/cache/cookbooks/Oracle11G/recipes/testSplit.rb:3:in block (2 levels) in from_file' 
Resource Declaration: --------------------- 
# In /var/chef/cache/cookbooks/Oracle11G/recipes/testSplit.rb 
1: bash "beforeTest" do 
2: code lazy{ 
3: echo "#{node[:oracle][:asm][:test]}" 
4: } 
5: end

请您帮忙在 bash 中应该如何使用惰性?如果不懒,还有其他选择吗?

【问题讨论】:

标签: ruby chef-infra lazy-evaluation recipe


【解决方案1】:
bash "beforeTest" do   
  code lazy { "echo #{node[:oracle][:asm][:test]}" }   
end

您应该引用命令以使插值起作用;如果没有,ruby 将搜索 echo 命令,这在 ruby​​ 上下文中是未知的(因此您在日志中得到错误)。

警告:惰性必须是整个资源属性;像这样不会工作:

bash "beforeTest" do    
  code "echo node asm test is: #{lazy { node[:oracle][:asm][:test]} }"  
end

惰性求值需要一段 ruby​​ 代码,如 here 所述

使用log 资源可能会获得更好的结果,如下所示:

log "print before" do
  message lazy { "node asm test is #{node[:oracle][:asm][:test]}" }
end

【讨论】:

    【解决方案2】:

    在想出 lambda 表达式之前,我一直在努力解决这个问题。但是仅仅使用 lambda 并没有帮助我。所以我想到了同时使用 lambda 和惰性求值。虽然 lambda 已经是延迟加载,但在编译厨师食谱时,调用 lambda 表达式的资源仍在评估中。所以为了防止它被评估(不知何故),我把它放在一个惰性评估字符串中。

    lambda 表达式

    app_version = lambda{`cat version`}
    

    然后是资源块

    file 'tmp/validate.version' do
        user 'user'
        group 'user_group'
        content lazy { app_version.call }
        mode '0755'
    end
    

    希望这也可以帮助其他人:) 或者如果您有更好的解决方案,请告诉我:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-27
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 2021-05-31
      相关资源
      最近更新 更多