我有以下问题,我认为这是因为我不理解总厨LWRP中的可变范围。
你可以看看problems的食谱,然后用厨房来测试它的行为。
尽管我定义了以下非常相似的资源,但我认识到,属性“colors”继承了以前定义的资源。属性‘color’默认为“‘蓝色’”,而在提供程序中,元素‘洋红’被添加到数组中。但是,第二资源和第三资源从前一个资源继承整个数组。我觉得很奇怪..。
菜谱/default.rb中的资源定义:
chef_problems_problem1 "test1" do
address "myaddress1"
action :install
end
chef_problems_problem1 "test2" do
address "myaddress2"
action :install
end
chef_problems_problem1 "test3" do
address "myaddress3"
action :install
end在厨房的输出集中,您可以看到变量new_resource.colors继承了以前资源的值:
* chef_problems_problem1[test1] action install
new_resource.colors at the beginning: ["blue"]
Values of local variables:
address: myaddress1
colors: ["blue"]
adding magenta to local variable colors
colors after adding magenta: ["blue", "magenta"]
new_resource.colors at the end: ["blue", "magenta"]
* chef_problems_problem1[test2] action install
new_resource.colors at the beginning: ["blue", "magenta"]
Values of local variables:
address: myaddress2
colors: ["blue", "magenta"]
adding magenta to local variable colors
colors after adding magenta: ["blue", "magenta", "magenta"]
new_resource.colors at the end: ["blue", "magenta", "magenta"]
* chef_problems_problem1[test3] action install
new_resource.colors at the beginning: ["blue", "magenta", "magenta"]
Values of local variables:
address: myaddress3
colors: ["blue", "magenta", "magenta"]
adding magenta to local variable colors
colors after adding magenta: ["blue", "magenta", "magenta", "magenta"]
new_resource.colors at the end: ["blue", "magenta", "magenta", "magenta"]也许你能帮我找出问题在哪里。
发布于 2015-05-11 14:38:10
看起来,默认值将传递给每个新资源,而不是对每个新资源进行克隆。因此,数组(而不是其内容)是默认值。如果向该数组添加内容,则预期使用默认属性值的每个提供程序都将拥有完整的数组。
就个人而言,它认为这是一些意想不到的行为。我认为您会将每个新资源都传递为默认的克隆,但这里似乎并非如此。现在,如果将一个新数组传递给资源定义中的color属性,则不会看到相同的效果。
然而,这不是一个范围问题。这是一个如何将默认值传递给资源的每个新实例的问题。
约翰的最新消息:
事实证明,这个问题已经在https://tickets.opscode.com/browse/CHEF-4608中讨论过了,但没有解决。您可以使用此票证中描述的解决方案,或者只需在提供程序操作中创建一个新数组,如
colors = Array.new(new_resource.colors)然后使用新的数组,不要接触原始属性。
https://stackoverflow.com/questions/30150766
复制相似问题