filtering ip in chef

84 Views Asked by At

I got a data-center kind of setup at my place. So basically I am creating a logstash conf file where it will use the filter parameter to include the datacenter name

for eg:-

10.21.53.x :- APP1 {10.21.53. belongs to APP1 datacenter and so on}
10.92.252.x :- APP2
10.23.252.x :- APP3

I am trying to write an erb template where 
>>if the ip address matches "10.21.53.x" the variable should be set as APP1
>>if the ip address matches "10.92.252.x" the variable should be set as APP2
>>if the ip address matches "10.23.252.x" the variable should be set as APP3
1

There are 1 best solutions below

0
On

i used the concept of using split and it seems to have worked.

>>> a
'192.168.23.23'
>>> a
'192.168.23.23'
>>> a.split()
['192.168.23.23']
>>> a.split(".")
['192', '168', '23', '23']
>>> a.split(".")[0]
'192'
>>> a.split(".")[1]
'168'
>>> if a.split(".")[1] == 168:
...  print "hey"
...
>>> if a.split(".")[1] == '168':
...  print "hey"
...
hey

used join again to compare all three octets of ip

>>> ".".join(a.split(".")[:3])
'192.168.23'

used this code to write in chef-shell and tested accordingly

node["network"]["interfaces"]["eth1"]["addresses"].select { |address, data| data["family"] == "inet" }.keys.join.split(".")[0,3].join(".")