首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >厨师LWRP中的可变范围

厨师LWRP中的可变范围
EN

Stack Overflow用户
提问于 2015-05-10 11:19:28
回答 1查看 495关注 0票数 0

我有以下问题,我认为这是因为我不理解总厨LWRP中的可变范围。

你可以看看problems的食谱,然后用厨房来测试它的行为。

尽管我定义了以下非常相似的资源,但我认识到,属性“colors”继承了以前定义的资源。属性‘color’默认为“‘蓝色’”,而在提供程序中,元素‘洋红’被添加到数组中。但是,第二资源和第三资源从前一个资源继承整个数组。我觉得很奇怪..。

菜谱/default.rb中的资源定义:

代码语言:javascript
复制
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继承了以前资源的值:

代码语言:javascript
复制
     * 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"]

也许你能帮我找出问题在哪里。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-11 14:38:10

看起来,默认值将传递给每个新资源,而不是对每个新资源进行克隆。因此,数组(而不是其内容)是默认值。如果向该数组添加内容,则预期使用默认属性值的每个提供程序都将拥有完整的数组。

就个人而言,它认为这是一些意想不到的行为。我认为您会将每个新资源都传递为默认的克隆,但这里似乎并非如此。现在,如果将一个新数组传递给资源定义中的color属性,则不会看到相同的效果。

然而,这不是一个范围问题。这是一个如何将默认值传递给资源的每个新实例的问题。

约翰的最新消息:

事实证明,这个问题已经在https://tickets.opscode.com/browse/CHEF-4608中讨论过了,但没有解决。您可以使用此票证中描述的解决方案,或者只需在提供程序操作中创建一个新数组,如

代码语言:javascript
复制
colors = Array.new(new_resource.colors)

然后使用新的数组,不要接触原始属性。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30150766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档