Debian Codegraph @

lib/ansible/module_utils/facts/network/aix.py

raw
1 # This file is part of Ansible
2 #
3 # Ansible is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # Ansible is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
15
16 from __future__ import annotations
17
18 import re
19
20 from ansible.module_utils.facts.network.base import NetworkCollector
21 from ansible.module_utils.facts.network.generic_bsd import GenericBsdIfconfigNetwork
22
23
24 class AIXNetwork(GenericBsdIfconfigNetwork):
25 """
26 This is the AIX Network Class.
27 It uses the GenericBsdIfconfigNetwork unchanged.
28 """
29 platform = 'AIX'
30
31 def get_default_interfaces(self, route_path):
32 interface = dict(v4={}, v6={})
33
34 netstat_path = self.module.get_bin_path('netstat')
35 if netstat_path is None:
36 return interface['v4'], interface['v6']
37
38 rc, out, err = self.module.run_command([netstat_path, '-nr'])
39
40 lines = out.splitlines()
41 for line in lines:
42 words = line.split()
43 if len(words) > 1 and words[0] == 'default':
44 if '.' in words[1]:
45 interface['v4']['gateway'] = words[1]
46 interface['v4']['interface'] = words[5]
47 elif ':' in words[1]:
48 interface['v6']['gateway'] = words[1]
49 interface['v6']['interface'] = words[5]
50
51 return interface['v4'], interface['v6']
52
53 # AIX 'ifconfig -a' does not have three words in the interface line
54 def get_interfaces_info(self, ifconfig_path, ifconfig_options='-a'):
55 interfaces = {}
56 current_if = {}
57 ips = dict(
58 all_ipv4_addresses=[],
59 all_ipv6_addresses=[],
60 )
61
62 uname_rc = uname_out = uname_err = None
63 uname_path = self.module.get_bin_path('uname')
64 if uname_path:
65 uname_rc, uname_out, uname_err = self.module.run_command([uname_path, '-W'])
66
67 rc, out, err = self.module.run_command([ifconfig_path, ifconfig_options])
68
69 for line in out.splitlines():
70
71 if line:
72 words = line.split()
73
74 # only this condition differs from GenericBsdIfconfigNetwork
75 if re.match(r'^\w*\d*:', line):
76 current_if = self.parse_interface_line(words)
77 interfaces[current_if['device']] = current_if
78 elif words[0].startswith('options='):
79 self.parse_options_line(words, current_if, ips)
80 elif words[0] == 'nd6':
81 self.parse_nd6_line(words, current_if, ips)
82 elif words[0] == 'ether':
83 self.parse_ether_line(words, current_if, ips)
84 elif words[0] == 'media:':
85 self.parse_media_line(words, current_if, ips)
86 elif words[0] == 'status:':
87 self.parse_status_line(words, current_if, ips)
88 elif words[0] == 'lladdr':
89 self.parse_lladdr_line(words, current_if, ips)
90 elif words[0] == 'inet':
91 self.parse_inet_line(words, current_if, ips)
92 elif words[0] == 'inet6':
93 self.parse_inet6_line(words, current_if, ips)
94 else:
95 self.parse_unknown_line(words, current_if, ips)
96
97 # don't bother with wpars it does not work
98 # zero means not in wpar
99 if not uname_rc and uname_out.split()[0] == '0':
100
101 if current_if['macaddress'] == 'unknown' and re.match('^en', current_if['device']):
102 entstat_path = self.module.get_bin_path('entstat')
103 if entstat_path:
104 rc, out, err = self.module.run_command([entstat_path, current_if['device']])
105 if rc != 0:
106 break
107 for line in out.splitlines():
108 if not line:
109 pass
110 buff = re.match('^Hardware Address: (.*)', line)
111 if buff:
112 current_if['macaddress'] = buff.group(1)
113
114 buff = re.match('^Device Type:', line)
115 if buff and re.match('.*Ethernet', line):
116 current_if['type'] = 'ether'
117
118 # device must have mtu attribute in ODM
119 if 'mtu' not in current_if:
120 lsattr_path = self.module.get_bin_path('lsattr')
121 if lsattr_path:
122 rc, out, err = self.module.run_command([lsattr_path, '-El', current_if['device']])
123 if rc != 0:
124 break
125 for line in out.splitlines():
126 if line:
127 words = line.split()
128 if words[0] == 'mtu':
129 current_if['mtu'] = words[1]
130 return interfaces, ips
131
132 # AIX 'ifconfig -a' does not inform about MTU, so remove current_if['mtu'] here
133 def parse_interface_line(self, words):
134 device = words[0][0:-1]
135 current_if = {'device': device, 'ipv4': [], 'ipv6': [], 'type': 'unknown'}
136 current_if['flags'] = self.get_options(words[1])
137 current_if['macaddress'] = 'unknown' # will be overwritten later
138 return current_if
139
140
141 class AIXNetworkCollector(NetworkCollector):
142 _fact_class = AIXNetwork
143 _platform = 'AIX'