在配方中压倒一切的属性 [英] Overriding attributes in the recipe

查看:166
本文介绍了在配方中压倒一切的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个食谱默认属性:

Let's say I have a default attribute in a cookbook:

default.nginx_upstreams = {
    'service1' => ['service1.server.com'],
    'service2' => ['service2.server.com'],
}

然后,它被修改,并在角色和环境重写,直到它终于到达我的食谱。在那里,我计算一些额外的服务,我想补充的属性。如果我做这样的事情:

Then it gets modified and overridden in roles and environments until it finally gets to my recipe. There, I compute some additional services that I would like to add to the attribute. If I do something like this:

node.nginx_upstreams.merge! {'service3' => ['service3.server.com']}

然后当我尝试使用属性在我的模板,我得到一个的零未定义的方法'各':在我的模板NilClass 当我尝试做

<% node.nginx_upstreams.each do |name, servers| %>

另外,我也得到了警告:没有指定precedence设置属性是pcated德$ P $,将在厨师11.0删除。乐于助人的警告告诉我如何在正常的precedence设置属性(显然,使用 node.set [钥匙] =值,但并没有告诉我如何指定默认或覆盖属性。

Plus, I also get a WARN: Setting attributes without specifying a precedence is deprecated and will be removed in Chef 11.0. The helpful warning tells me how to set attributes at normal precedence (apparently, using node.set["key"] = "value", but doesn't tell me how to specify default or override attributes.

我可以做这样的事情解决这个问题:

I can get around this problem by doing something like this:

upstreams = node.nginx_upstreams.to_hash
upstreams.merge! {'service3' => ['service3.server.com']}

template "nginx_config" do
    variables({:upstreams=>upstreams})
end

但感觉就像一个黑客攻击。我找不到 node.set)的任何文件(之外的此页面,这也预示着你可以设置正常和覆盖在配方属性,但是没有说怎么样。

but that feels like a hack. I cannot find any documentation on node.set() beyond this page, which also indicates that you can set both normal and override attributes in the recipe but doesn't say how.

所以... ...你如何正确设置属性从配方内(即获得与其他内容一起深合并)?什么是 node.set()调用实际上做的,我可以告诉它的precedence我想在合并?

So... how do you properly set attributes (that get deep-merged along with everything else) from inside the recipe? What does the node.set() call actually do, and can I tell it the precedence I want to merge at?

推荐答案

default.nginx_upstreams 相同默认的[:nginx_upstreams] 默认['nginx_upstreams'] - 该约定是使用后两者的1。而当你进一步使用字符串,在这里使用他们。

default.nginx_upstreams is the same as default[:nginx_upstreams] and default['nginx_upstreams'] - the convention is to use 1 of the latter two. And as you are using strings further, use them here too.

这就是你的init nginx_upstreams 在属性文件是一样的做这种方式:

The way you init nginx_upstreams in attribute file is the same as doing it this way:

default['nginx_upstreams']['service1'] = ['service1.server.com']
default['nginx_upstreams']['service2'] = ['service2.server.com']

和你没有给init 默认['nginx_upstreams'] = {} 之前。这些都不是哈希值,但属性,他们是聪明很多。 :)

And you don't have to init default['nginx_upstreams'] = {} before that. These are not hashes, but attributes, and they are a lot smarter. :)

修改从里面的配方就是这样做的属性:

Modifying attributes from inside the recipe is done like that:

node.default['nginx_upstreams']['service3'] = ['service3.server.com']

您可以使用设置覆盖而不是默认这里,如果你需要改变precedence。省略precedence名(节点['nginx_upstreams'] node.nginx_upstreams )将使用设置 precedence。但是,这是德precated并很快被删除 - 这是警告的全部意义所在。
退房href=\"http://docs.chef.io/attributes.html\">关于属性手册页的

You can use set or override instead of default here, if you need to change precedence. Omitting the precedence name (node['nginx_upstreams'] or node.nginx_upstreams) will use the set precedence. But this is deprecated and soon to be removed - that's what the warning is all about. Check out the manual page about attributes, because everything is actually there.

这篇关于在配方中压倒一切的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆