Chef中的动态角色属性 [英] Dynamic Role Attributes in Chef

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

问题描述

我希望Chef Cookbook network_interfaces 对于我的每个节点都具有ip地址,网络掩码等的动态值。适用于我的内容如下:

I would like the Chef cookbook network_interfaces to have dynamic values for ip addresses, netmasks and alike for each of my nodes. What works for me is the following:

db_role.rb(block1):

db_role.rb (block1):

override_attributes(
  "network_interfaces" => {
      :device => 'eth0',
      :address => '123.123.123.123',
  }
)

但这不是很动态。我的想法是将ip地址(,网络掩码等)提交给刀具引导程序上的每个节点。

But that is not very dynamic. My idea was to submit the ip address(, netmask, etc.) to each node on knife bootstrap.

然后该节点将如下所示(block2):

The node would then look like so (block2):

{
    "normal": {
      "network_interfaces" => {
          "device" : "eth0",
          "address" : "123.123.123.123"
      }
    },
    "name": "foobar",
    "run_list": [
       "recipe[zsh]",
       "role[networking_interfaces]"
    ]
}

不幸的是, network_interfaces 食谱默认情况下不会选择这些值。我的想法是像这样在角色定义中引用block2中显示的特定于节点的属性:

Unfortunately the network_interfaces cookbook does not pick up those values by default. My idea was to reference the node specific attributes shown in block2 in the roles definition like so:

override_attributes(
  "network_interfaces" => {
      :device => node['network_interfaces']['device'],
      :address => node['network_interfaces']['address'],
  }
)

这行不通,因为它显然不是json,Chef可以无法处理角色文件中动态分配的值。

This does not work because it is not json obviously and Chef can not handle dynamically allocated values in roles files.

如何实现运行 network_interfaces 配方并传递我的节点

How can I achieve to run the network_interfaces recipe and pass my node specific values to it?

推荐答案

如果通过刀添加 normal 属性bootstrap -j…,并将 override 属性保留在角色中, override 将接管(请参见 http://docs.opscode.com/essentials_node_object_attributes_precedence.html 获取属性优先级的完整列表)。如果您在运行刀具引导程序 db_role.rb 中删除​​了 override_attributes c>,或将其更改为 default_attributes ,然后在节点属性中设置IP即可。

If you add normal attributes via knife bootstrap -j …, and leave override attributes in the role, the override will take over (see http://docs.opscode.com/essentials_node_object_attributes_precedence.html for a complete list of attribute precedence). If you have deleted override_attributes from db_role.rb before running knife bootstrap, or changed it to default_attributes, then setting IP in node attributes should have worked.

最后一个代码段将无法正常工作:角色是Chef服务器上的静态JSON文档,并且在将角色上传到服务器时,Ruby仅由解释一次(http://docs.opscode .com / essentials_roles_formats.html)。您无法从角色的Ruby代码中引用节点的属性,因为它甚至在接触任何节点之前都已编译为JSON。如果您想尝试类似的方法,则需要使用自定义食谱(例如, my_network_interfaces ),其配方应类似于以下内容:

The last snippet won't work: roles are static JSON documents on the Chef server, and Ruby is only interpreted once by knife when uploading role to the server (http://docs.opscode.com/essentials_roles_formats.html). You can't refer to node's attributes from role's Ruby code, as it's compiled to JSON before it even touches any node. If you want to try a similar approach, you need to use a custom cookbook (say, my_network_interfaces) with a recipe that would look somewhat like this:

node.set['network_interface']['device'] = …
node.set['network_interface']['address'] = …
include_recipe 'network_interfaces'

这样,您将使用 network_interfaces 作为库菜谱,由您的应用程序菜谱 my_network_interfaces 调用,该菜谱实现了所需的任何逻辑。根据您的问题,我无法建议您如何计算设备和地址,因为您的示例只是尝试复制相同的属性,因此不能操作。据我了解您要实现的目标,您希望角色中有 default_attributes ,并将具有 normal 属性的特定JSON传递给刀具引导程序覆盖默认设置。

This way, you'd use network_interfaces as a "library" cookbook, called by your "application" cookbook my_network_interfaces which implements whatever logic you need. From your question, I can't suggest how would you compute device and address, as your example just tries to copy same attributes, which is a no-op. As far as I understand what you want to achieve, you want to have default_attributes in the role, and pass specific JSON with normal attributes to knife bootstrap to override the defaults.

这篇关于Chef中的动态角色属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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