跨配方的访问属性 [英] Access Attributes Across Recipes

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

问题描述

是否可以配置可通过配方访问的属性?如果是,怎么办?我已经为每个食谱配置了属性,但是我想消除很多重复。

Is it possible to configure attributes that are accessible across recipes? If yes, how? I've configured attributes for each recipe, but there is significant repetition I'd like to eliminate.

推荐答案

Chef的工作方式是,在融合开始时,Chef遍历所有具有将在其上运行的食谱的食谱节点(直接在运行列表上,或通过 include_recipe 引用)。然后,对于每本食谱,厨师将读取存储在属性文件夹中的属性文件的 all ,并将属性添加到节点哈希。

The way Chef works is that at the start of a convergence Chef iterates through all cookbooks that have recipes that will be run on the node (either directly on the run list or referenced via include_recipe). For each cookbook Chef will then read in all of the attribute files stored in the attributes folder and add the attributes to the node hash.

此处要注意的重要一点是,这些属性与您正在执行的配方没有任何关系,所有配方都会看到

The important thing to note here is that the attributes bear no relation to the recipes you are executing, and all recipes will see the same view of the attributes that were loaded.

您的描述有点含糊,但是从我的搜集中,您可以获得一本食谱,其中包含一些食谱,每个食谱您正在创建相应的属性文件的配方。在每个这些属性文件中,您都重新定义了相同的属性(例如 default ['myapp'] ['foo'] = blah )。

Your description is a bit vague, but from what I can gather you have a cookbook with a few recipes, and for each recipe you are creating a corresponding attribute file. Within each of these attribute files you're re-defining the same attribute (e.g. default['myapp']['foo'] = "blah").

由于我在开始时描述了此过程,因此这不是必需的。在融合时,Chef将读取您所有的属性文件,而不管您运行的是什么配方。这意味着要加载的第二个(或第三个或第四个等)属性文件将简单地覆盖 node ['myapp'] ['foo'] 属性

This is not-necessary due to the process I described at the start. At convergence time Chef will read through all of your attribute files regardless of what recipes you are running. This means that the second (or third, or fourth, etc) attribute file to be loaded will simply overwrite the node['myapp']['foo'] attribute that was set by the previously loaded attribute file.

在这种情况下,常见的模式是使用 default.rb 属性文件,该文件定义了所有配方将使用的所有通用属性。如果需要,您可以将特定于配方/组件的属性移至其自己的属性文件。

In this situation, the common pattern is to have a default.rb attribute file which defines all of the common attributes that will be used by all of your recipes. If needed, you can then move your recipe/component-specific attributes to their own attribute file.

在这种情况下要注意的一件事是Chef会按字母顺序加载属性文件。如果您的食谱专用属性文件(例如 client.rb )引用了 default.rb 中定义的属性之一,那么您将需要使用 include_attribute 指令,以使Chef在 client.rb 之前加载 default.rb ,否则 default.rb 的所有属性仍然为零。

One thing to be aware of in this situation is that Chef loads attribute files in alphabetical order. If your recipe-specific attribute file (e.g. client.rb) references one of the attributes defined in default.rb, then you will need to use the include_attribute directive in order to get Chef to load default.rb before your client.rb, otherwise all of the attributes from default.rb will still be nil.

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

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