Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Role Attributes in Chef

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):

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

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

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]"
    ]
}

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'],
  }
)

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

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

like image 239
j7nn7k Avatar asked Feb 17 '23 21:02

j7nn7k


1 Answers

Maciej, I followed your suggestions. I upload the custom parameters like IP, broadcast, etc with the -j option upon bootstrap.

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"}}'     

Plus I wrote a custom recipe to achieve the dynamic matching. Here's the code:

#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
like image 104
j7nn7k Avatar answered Feb 27 '23 04:02

j7nn7k