考虑以下代码:
node["cloudwatch"]["alarms"].each do |metric,keys|
script_name = "/root/.cloudwatch/create_cloudwatch_alarm-#{metric}.sh"
template script_name do
source "cloudwatch/create_cloudwatch_EC2_alarm.sh.erb"
owner "root"
group "root"
mode "0655"
variables(
:metric => metric,
:description => keys["description"],
:statistic => keys["statistic"],
:comparison => keys["comparison"],
:threshold => keys["threshold"],
:evalperiods => keys["evalperiods"],
:period => keys["period"]
)
notifies :run, "bash[populate-create_alarms]", :immediately
end
bash "populate-create_alarms" do
user "root"
cwd "/root/.cloudwatch/"
code <<-EOH
echo #{script_name} >> ./create_alarms.sh
EOH
action :nothing
end
end
运行正常,但在日志中我多次收到以下警告:
WARN: Cloning resource attributes for bash[populate-create_alarms] from prior resource (CHEF-3694)
WARN: Previous bash[populate-create_alarms]: /var/chef-solo/cookbooks/soapp/recipes/cloudwatch.rb:220:in `block in from_file'
WARN: Current bash[populate-create_alarms]: /var/chef-solo/cookbooks/soapp/recipes/cloudwatch.rb:220:in `block in from_file'
我知道为什么会收到警告,但我不知道如何避免。显然"populate-create_alarms"
不应该在node["cloudwatch"]["alarms"].each
块内,但我不知道如何传递#{script_name}
给"populate-create_alarms"
。有人能告诉我正确的方法吗?
答案1
使bash
资源名称唯一,例如通过以下代码:
bash "populate-create_alarms-#{metric}" do
..
end