Chef中的动态角色属性

j7n*_*n7k 4 ruby chef-infra

我希望Chef cookbook network_interfaces为每个节点提供ip地址,netmasks等的动态值.对我有用的是:

db_role.rb(block1):

override_attributes(
  "network_interfaces" => {
      :device => 'eth0',
      :address => '123.123.123.123',
  }
)
Run Code Online (Sandbox Code Playgroud)

但这不是很有活力.我的想法是将ip地址(,网络掩码等)提交给每个节点knife bootstrap.

然后该节点看起来像这样(块2):

{
    "normal": {
      "network_interfaces" => {
          "device" : "eth0",
          "address" : "123.123.123.123"
      }
    },
    "name": "foobar",
    "run_list": [
       "recipe[zsh]",
       "role[networking_interfaces]"
    ]
}
Run Code Online (Sandbox Code Playgroud)

不幸的network_interfaces是,默认情况下,食谱不会提取这些值.我的想法是在角色定义中引用block2中显示的节点特定属性,如下所示:

override_attributes(
  "network_interfaces" => {
      :device => node['network_interfaces']['device'],
      :address => node['network_interfaces']['address'],
  }
)
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为它显然不是json,而Chef无法处理角色文件中动态分配的值.

如何运行network_interfaces配方并将特定于节点的值传递给它?

j7n*_*n7k 6

Maciej,我听从你的建议.我在bootstrap上使用-j选项上传自定义参数,如IP,广播等.

knife bootstrap IP_OF_THE_NODE -r 'role[main_application]' -j '{ "network_interfaces" : {"device" : "eth1.0000", "type" : "static", "address" : "192.168.1.1", "netmask" : "255.255.255.0", "broadcast" : "192.168.0.255","gateway": "192.168.0.1"}}'     
Run Code Online (Sandbox Code Playgroud)

另外,我写了一个自定义配方来实现动态匹配.这是代码:

#setup a custom network config per vm
network_interfaces node["network_interfaces"]["device"] do
  Chef::Log.info("Compiling network_interfaces")
  target node["network_interfaces"]["address"]
  mask node["network_interfaces"]["netmask"]
  broadcast node["network_interfaces"]["broadcast"]
  gateway node["network_interfaces"]["gateway"]
  custom node["network_interfaces"]["custom"]
  action :save
end
Run Code Online (Sandbox Code Playgroud)

  • @ j7nn7k提供的内容是正确的.也许你对'chef-client`选项@AkD感到困惑,请参阅https://tickets.opscode.com/browse/CHEF-3965#comment-33163 (2认同)