Фильтрация ip в шеф-поваре

У меня есть дата-центр, настроенный на моем месте. Поэтому в основном я создаю файл conf logstash, где он будет использовать параметр фильтра для включения имени центра данных.

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 ответ

Я использовал концепцию использования сплита, и это, кажется, сработало.

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

снова использовал join для сравнения всех трех октетов ip

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

использовал этот код для записи в chef-shell и соответственно протестировал

узел ["сеть"] ["интерфейсы"]["eth1"]["адреса"]. выберите { | адрес, данные | data["family"] == "inet" }.keys.join.split(".")[0,3].join(".")

Другие вопросы по тегам