Chef LWRP 中的可变范围
variable scope in chef LWRPs
我遇到以下问题,我认为这是因为我不了解 Chef LWRP 中的变量范围。
您可以查看 https://github.com/jkleinlercher/chef_problems 上的食谱,并使用厨房简单地测试一下行为。
尽管我定义了以下非常相似的资源,但我认识到属性 'colors' 继承自先前定义的资源。属性 'colors' 默认为“['blue']”,并且在提供者中元素 'magenta' 被添加到数组中。但是,第二个和第三个资源从前一个资源继承了整个数组。这对我来说很奇怪......
资源定义在recipes/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
在 kitchen converge 的输出中,您会看到变量 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"]
也许你能帮我找出问题出在哪里。
默认值似乎已传递给每个新资源,但并未为每个新资源克隆。结果,数组(不是它的内容)是默认值。如果您向该数组添加内容,则预计每个使用默认属性值的提供者都将拥有完整的数组。
就我个人而言,它认为这是有些出乎意料的行为。我认为您会将每个新资源传递给默认资源的克隆,但这里似乎并非如此。现在,如果您将一个新数组传递给资源定义中的颜色属性,那么您将看不到相同的效果。
但这不是范围问题。这是默认值如何传递给资源的每个新实例的问题。
来自约翰内斯的更新:
原来这个问题在https://tickets.opscode.com/browse/CHEF-4608中已经讨论过,但没有修复。您可以使用此票证中描述的解决方法,或者简单地在提供者操作中创建一个新数组,如
colors = Array.new(new_resource.colors)
然后使用新数组,不要触及原始属性。
我遇到以下问题,我认为这是因为我不了解 Chef LWRP 中的变量范围。
您可以查看 https://github.com/jkleinlercher/chef_problems 上的食谱,并使用厨房简单地测试一下行为。
尽管我定义了以下非常相似的资源,但我认识到属性 'colors' 继承自先前定义的资源。属性 'colors' 默认为“['blue']”,并且在提供者中元素 'magenta' 被添加到数组中。但是,第二个和第三个资源从前一个资源继承了整个数组。这对我来说很奇怪......
资源定义在recipes/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
在 kitchen converge 的输出中,您会看到变量 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"]
也许你能帮我找出问题出在哪里。
默认值似乎已传递给每个新资源,但并未为每个新资源克隆。结果,数组(不是它的内容)是默认值。如果您向该数组添加内容,则预计每个使用默认属性值的提供者都将拥有完整的数组。
就我个人而言,它认为这是有些出乎意料的行为。我认为您会将每个新资源传递给默认资源的克隆,但这里似乎并非如此。现在,如果您将一个新数组传递给资源定义中的颜色属性,那么您将看不到相同的效果。
但这不是范围问题。这是默认值如何传递给资源的每个新实例的问题。
来自约翰内斯的更新:
原来这个问题在https://tickets.opscode.com/browse/CHEF-4608中已经讨论过,但没有修复。您可以使用此票证中描述的解决方法,或者简单地在提供者操作中创建一个新数组,如
colors = Array.new(new_resource.colors)
然后使用新数组,不要触及原始属性。