配方中属性的动态用法

问题描述 投票:0回答:2

我正在尝试增加值并在配方中动态地在另一个资源中使用,但仍然无法做到这一点。

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 lazy-evaluation recipe
2个回答
1
投票
bash "beforeTest" do   
  code lazy { "echo #{node[:oracle][:asm][:test]}" }   
end

您应该引用命令以使插值生效;如果不是,ruby会搜索一个echo命令,这在ruby上下文中是未知的(因此您在日志中得到的错误)。

警告:懒惰必须针对整个资源属性;像这样的东西[[WO N'T work:

bash "beforeTest" do code "echo node asm test is: #{lazy { node[:oracle][:asm][:test]} }" end
懒惰的评估需要一段红宝石代码,如here所述>

使用这样的log资源可能会得到更好的结果:

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


0
投票
我一直在努力解决这个问题,直到我想到了lambda表达式。但是,仅仅使用lambda并不能帮助我。因此,我想到了同时使用lambda和lazy评估。尽管lambda已经是延迟加载,但是在编译厨师食谱时,仍在评估您调用lambda表达式的资源。因此,为了防止对其进行评估(以某种方式),我将其放在惰性评估字符串中。
© www.soinside.com 2019 - 2024. All rights reserved.