Skip to content

Commit c6d4bef

Browse files
committed
Add a parser
1 parent 3c2d31c commit c6d4bef

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

parser.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env python
2+
3+
import debinterface
4+
import debinterface.interfaces
5+
import debinterface.adapter
6+
import sys
7+
8+
if __name__ == "__main__":
9+
intfile = sys.argv[1]
10+
interfaces = debinterface.interfaces.Interfaces(interfaces_path=intfile)
11+
interfaces.updateAdapters()
12+
13+
adapters = interfaces.adapters
14+
options = {
15+
'hotplug'
16+
, 'auto'
17+
, 'name'
18+
, 'hwaddress'
19+
, 'address'
20+
, 'netmask'
21+
, 'network'
22+
, 'broadcast'
23+
, 'gateway'
24+
, 'bridge-opts'
25+
, 'addrFam'
26+
, 'source'
27+
, 'nameservers'
28+
, 'unknown'
29+
, 'up'
30+
, 'pre-up'
31+
, 'post-up'
32+
, 'down'
33+
, 'post-down'
34+
}
35+
36+
print "network_managed_by_ansible: True"
37+
print "network_manage_devices: True"
38+
print "network_host_interfaces:"
39+
for apt in adapters:
40+
attrs = apt.export(options)
41+
if attrs['auto'] == None:
42+
attrs['auto'] = True
43+
if attrs['name'] == 'lo' and attrs['source'] == 'loopback':
44+
continue
45+
else:
46+
print " - device: %s" % attrs['name']
47+
print " description: %s network configs" % attrs['name']
48+
print " auto: %s" % attrs['auto']
49+
print " family: %s" % attrs['addrFam']
50+
print " method: %s" % attrs['source']
51+
if attrs['address']:
52+
print " address: %s" % attrs['address']
53+
if attrs['unknown'] and 'hwaddress' in attrs['unknown'] and attrs['unknown']['hwaddress']:
54+
print " hwaddress: %s" % attrs['unknown']['hwaddress']
55+
if 'network' in attrs and attrs['network']:
56+
print " network: %s" % attrs['network']
57+
if attrs['broadcast']:
58+
print " broadcast: %s" % attrs['broadcast']
59+
if 'netmask' in attrs and attrs['netmask']:
60+
print " netmask: %s" % attrs['netmask']
61+
if 'gateway' in attrs and attrs['gateway']:
62+
print " gateway: %s" % attrs['gateway']
63+
if 'nameservers' in attrs and attrs['nameservers']:
64+
print " nameservers:"
65+
for ns in attrs['nameservers']:
66+
print " - %s" % ns
67+
if attrs['unknown'] and 'dns-nameservers' in attrs['unknown'] and attrs['unknown']['dns-nameservers']:
68+
print " nameservers: %s" % attrs['unknown']['dns-nameservers']
69+
if attrs['unknown']:
70+
if 'dns-search' in attrs['unknown'] and attrs['unknown']['dns-search']:
71+
print " dns_search: %s" % attrs['unknown']['dns-search']
72+
else:
73+
print " dns_search: %s" % argv[2]
74+
if 'up' in attrs and attrs['up']:
75+
print " up:"
76+
for cmd in attrs['up']:
77+
if cmd.startswith('ip addr add 10.'):
78+
print " - /usr/local/sbin/do_anchor_ip.sh %s" % attrs['name']
79+
else:
80+
print " - %s" % cmd
81+
if 'pre-up' in attrs and attrs['pre-up']:
82+
print " pre-up:"
83+
for cmd in attrs['pre-up']:
84+
print " - %s" % cmd
85+
if attrs['unknown'] and len(attrs['unknown']) > 0:
86+
bond = {}
87+
vlan = {}
88+
for key in attrs['unknown']:
89+
if not key in ['hwaddress', 'address', 'netmask', 'network', 'broadcast', 'gateway', 'dns-search', 'dns-nameservers']:
90+
if key.startswith('bond-'):
91+
skey = key[5:]
92+
bond[skey] = attrs['unknown'][key]
93+
elif key.startswith('vlan-'):
94+
skey = key[5:]
95+
vlan[skey] = attrs['unknown'][key]
96+
else:
97+
print " %s: %s ## unknown option" % (key, attrs['unknown'][key])
98+
if len(bond) > 0:
99+
print " bond:"
100+
for key in bond:
101+
print " %s: %s" % (key, bond[key])
102+
if len(vlan) > 0:
103+
print " vlan:"
104+
for key in vlan:
105+
print " %s: %s" % (key, vlan[key])
106+
107+
if interfaces.includes:
108+
print "## WARNING: there are more configs for this host"

0 commit comments

Comments
 (0)