0

I'd like to know what would be the best way to populate a Chef attribute in a cookbook with the last octet of the IP address.

Here is how I do it now. It seems to work; however,I'd like to know how I can improve it.

default['application']['host_ip'] = node['network']['interfaces']['eth0']['addresses'].keys[1]
default['application']['app_id'] = node['application']['host_ip'].split('.')[-1]

Thanks!

Aaron Bandelli
  • 1,238
  • 2
  • 14
  • 16

1 Answers1

4

That looks fine. You might want some error handling since this will crash if there isn't an eth0, but that's up to you. You could also use node['ipaddress'] which is the IP on the default interface.

coderanger
  • 52,400
  • 4
  • 52
  • 75