【问题标题】:Chef error on attributes- string not matched属性上的厨师错误 - 字符串不匹配
【发布时间】:2015-01-29 03:29:43
【问题描述】:

我似乎无法绕过处理我的attributes/default.rb 文件中类似名称的属性的厨师错误。

我有两个属性:

default['test']['webservice']['https']['keyManagerPwd'] = 'password'
...
...
default['test']['webservice']['https']['keyManagerPwd']['type'] = 'encrypted'

请注意,直到最后一个括号 (['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'

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

  default['test']['1']['webservice']['https']['keyManagerPwd'] = 'password'
  ...
  ...
  default['test']['2']['webservice']['https']['keyManagerPwd']['type'] = 'encrypted'

通过将['1'] 和['2'] 放在那里,它可以解决问题。

我对 Chef 还很陌生,所以我认为这只是我忽略的一些简单的事情。有没有人有任何想法或建议?谢谢。

【问题讨论】:

    标签: ruby chef-infra


    【解决方案1】:

    简单的回答:你不能这样做。这不是 Chef 问题,也不是 ruby​​ 问题 - 这是大多数编程语言的普遍问题。

    让我们使用foo 作为变量,而不是冗长的default['test']['webservice']['https']['keyManagerPwd']。

    你实际上做的是

    1: foo = "password"
    2: foo['type'] = "encrypted"
    

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

    1: foo = "password"
    2: foo = {}
    3: foo['type'] = "encrypted"
    

    替代方法是使用

    foo['something'] = "password"
    foo['type'] = "encrypted"
    

    或者翻译成你的代码:

    default['test']['webservice']['https']['keyManagerPwd']['something'] = 'password'
    default['test']['webservice']['https']['keyManagerPwd']['type'] = 'encrypted'
    

    这应该可行。

    【讨论】:

    • 无法更好地解释 ;)
    • 谢谢。我毫不怀疑这是正在发生的事情,您的替代解决方案将起作用,因此我将遵循该建议。话虽如此,我有点不清楚foo 和default['test']['webservice']['https']['keyManagerPwd'] 是如何等效的。我将 foo 视为一个字符串,但后者本身就是一个哈希。
    • foo 和 default['test']['webservice']['https'] 是等价的。两者都可以是任何类型。您可以将任何内容放入 ruby​​ 中的哈希中。
    • (要清楚,Chef 在这里使用的是 AFAIK 不是普通的旧红宝石哈希,而是看起来像哈希并且感觉像哈希的东西,但被称为 mash - 但我建议你暂时忽略这个事实)
    • 感谢您的帮助和解释!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多