属性字符串上的Chef错误不匹配

Mik*_*ike 3 ruby chef-infra

我似乎无法解决在我的attributes/default.rb文件中处理类似命名属性的厨师错误.

我有2个属性:

default['test']['webservice']['https']['keyManagerPwd'] = 'password'
...
...
default['test']['webservice']['https']['keyManagerPwd']['type'] = 'encrypted'
Run Code Online (Sandbox Code Playgroud)

请注意,直到最后一个括号(['type']),名称是相同的.

我在模板和配方中的模板块中引用这些属性.当我去运行它时,我收到此错误:

==================================================[0m
I, [2015-01-28T13:36:43.668692 #7920]  INFO -- core-14-2-centos-65: 
[31mRecipe Compile Error
in /tmp/kitchen/cache/cookbooks/avx/attributes/default.rb[0m
I, [2015-01-28T13:36:43.669192 #7920]  INFO -- core-14-2-centos-65: 
=================================================================[0m
I, [2015-01-28T13:36:43.669192 #7920]  INFO -- core-14-2-centos-65: 
I, [2015-01-28T13:36:43.669692 #7920]  INFO -- core-14-2-centos-65:    
[0mIndexError[0m
I, [2015-01-28T13:36:43.669692 #7920]  INFO -- core-14-2-centos-65: -------
--[0m
I, [2015-01-28T13:36:43.669692 #7920]  INFO -- core-14-2-centos-65: string not matched[0m
I, [2015-01-28T13:36:43.670192 #7920]  INFO -- core-14-2-centos-65: 
I, [2015-01-28T13:36:43.670192 #7920]  INFO -- core-14-2-centos-65:
[0mCookbook Trace:[0m
I, [2015-01-28T13:36:43.670692 #7920]  INFO -- core-14-2-centos-65: --------[0m
I, [2015-01-28T13:46:05.101875 #8332]  INFO -- core-14-2-centos-65:     
[0m113>> default['webservice']['https']['keyManagerPwd']['type'] = 
'encrypted'
Run Code Online (Sandbox Code Playgroud)

当唯一的区别是结束时,似乎Chef无法区分2个属性.如果我通过在名称的前面放置一些独特的文本来修改相同的属性,则根本没有配方问题:

  default['test']['1']['webservice']['https']['keyManagerPwd'] = 'password'
  ...
  ...
  default['test']['2']['webservice']['https']['keyManagerPwd']['type'] = 'encrypted'
Run Code Online (Sandbox Code Playgroud)

通过放置['1']['2']那里,它解决了问题.

我对Chef很新,所以我觉得它只是简单的我忽略了.有没有人有任何想法或建议?谢谢.

Ste*_*ing 9

简单回答:你不能这样做.这不是厨师问题,也不是红宝石问题 - 这是大多数编程语言的普遍问题.

让我们foo用作变量而不是冗长default['test']['webservice']['https']['keyManagerPwd'].

你有效地做了什么

1: foo = "password"
2: foo['type'] = "encrypted"
Run Code Online (Sandbox Code Playgroud)

在第1行中,foo是一个字符串.在第2行中,它被视为散列(在其他一些语言中称为数组).第二行自动覆盖您的foo = "password"作业.它实际上是一样的

1: foo = "password"
2: foo = {}
3: foo['type'] = "encrypted"
Run Code Online (Sandbox Code Playgroud)

替代方案是使用

foo['something'] = "password"
foo['type'] = "encrypted"
Run Code Online (Sandbox Code Playgroud)

或者翻译成你的代码:

default['test']['webservice']['https']['keyManagerPwd']['something'] = 'password'
default['test']['webservice']['https']['keyManagerPwd']['type'] = 'encrypted'
Run Code Online (Sandbox Code Playgroud)

这应该工作.