0

我在我的地方有一个数据中心的设置。所以基本上我正在创建一个logstash conf文件,它将使用过滤器参数来包含数据中心名称

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
4

1 回答 1

0

我使用了使用拆分的概念,它似乎奏效了。

>>> 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 中编写并进行相应测试

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

于 2016-08-24T17:44:42.943 回答