我有两个 LWRP。第一个涉及创建磁盘卷、对其进行格式化并将其安装在虚拟机上,我们将此称为资源cloud_volume
。第二个资源(它的作用并不重要)需要新格式化卷的 UUID,这是必需的属性,我们将调用此资源foobar
.
资源cloud_volume
and foobar
用于类似下面的食谱中。
volumes.each do |mount_point, volume|
cloud_volume "#{mount_point}" do
size volume['size']
label volume['label']
action [:create, :initialize]
end
foobar "#{mount_point}" do
disk_uuid node[:volumes][mount_point][:uuid] # This is set by cloud_volume
action [:do_stuff]
end
end
所以,当我做厨师运行时,我得到了Required argument disk_identifier is missing!
例外。
经过一番挖掘后,我发现配方分两个阶段处理:编译阶段和执行阶段。看起来问题出在编译时,因为那是时间点node[:volumes][mount_point][:uuid]
未设置。
不幸的是我无法使用 OpsCode 的技巧here http://web.archive.org/web/20120226074250/http://wiki.opscode.com/display/chef/Evaluate+and+Run+Resources+at+Compile+Time由于在 cloud_volume LWRP 中使用通知(因此它会落入文档中显示的反模式)
所以,毕竟,我的问题是,有什么办法可以绕过价值的要求disk_uuid
在编译时就知道?
更干净的方法是使用惰性属性评估 http://docs.opscode.com/resource_common.html#lazy-attribute-evaluation。这将评估node[:volumes][mount_point][:uuid]
在执行期间而不是编译期间
foobar "#{mount_point}" do
disk_uuid lazy { node[:volumes][mount_point][:uuid] }
action [:do_stuff]
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)