Debian Codegraph @

lib/ansible/module_utils/facts/network/sunos.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 SunOSNetwork(GenericBsdIfconfigNetwork):
25 """
26 This is the SunOS Network Class.
27 It uses the GenericBsdIfconfigNetwork.
28
29 Solaris can have different FLAGS and MTU for IPv4 and IPv6 on the same interface
30 so these facts have been moved inside the 'ipv4' and 'ipv6' lists.
31 """
32 platform = 'SunOS'
33
34 # Solaris 'ifconfig -a' will print interfaces twice, once for IPv4 and again for IPv6.
35 # MTU and FLAGS also may differ between IPv4 and IPv6 on the same interface.
36 # 'parse_interface_line()' checks for previously seen interfaces before defining
37 # 'current_if' so that IPv6 facts don't clobber IPv4 facts (or vice versa).
38 def get_interfaces_info(self, ifconfig_path):
39 interfaces = {}
40 current_if = {}
41 ips = dict(
42 all_ipv4_addresses=[],
43 all_ipv6_addresses=[],
44 )
45 rc, out, err = self.module.run_command([ifconfig_path, '-a'])
46
47 for line in out.splitlines():
48
49 if line:
50 words = line.split()
51
52 if re.match(r'^\S', line) and len(words) > 3:
53 current_if = self.parse_interface_line(words, current_if, interfaces)
54 interfaces[current_if['device']] = current_if
55 elif words[0].startswith('options='):
56 self.parse_options_line(words, current_if, ips)
57 elif words[0] == 'nd6':
58 self.parse_nd6_line(words, current_if, ips)
59 elif words[0] == 'ether':
60 self.parse_ether_line(words, current_if, ips)
61 elif words[0] == 'media:':
62 self.parse_media_line(words, current_if, ips)
63 elif words[0] == 'status:':
64 self.parse_status_line(words, current_if, ips)
65 elif words[0] == 'lladdr':
66 self.parse_lladdr_line(words, current_if, ips)
67 elif words[0] == 'inet':
68 self.parse_inet_line(words, current_if, ips)
69 elif words[0] == 'inet6':
70 self.parse_inet6_line(words, current_if, ips)
71 else:
72 self.parse_unknown_line(words, current_if, ips)
73
74 # 'parse_interface_line' and 'parse_inet*_line' leave two dicts in the
75 # ipv4/ipv6 lists which is ugly and hard to read.
76 # This quick hack merges the dictionaries. Purely cosmetic.
77 for iface in interfaces:
78 for v in 'ipv4', 'ipv6':
79 combined_facts = {}
80 for facts in interfaces[iface][v]:
81 combined_facts.update(facts)
82 if len(combined_facts.keys()) > 0:
83 interfaces[iface][v] = [combined_facts]
84
85 return interfaces, ips
86
87 def parse_interface_line(self, words, current_if, interfaces):
88 device = words[0][0:-1]
89 if device not in interfaces:
90 current_if = {'device': device, 'ipv4': [], 'ipv6': [], 'type': 'unknown'}
91 else:
92 current_if = interfaces[device]
93 flags = self.get_options(words[1])
94 v = 'ipv4'
95 if 'IPv6' in flags:
96 v = 'ipv6'
97 if 'LOOPBACK' in flags:
98 current_if['type'] = 'loopback'
99 current_if[v].append({'flags': flags, 'mtu': words[3]})
100 current_if['macaddress'] = 'unknown' # will be overwritten later
101 return current_if
102
103 # Solaris displays single digit octets in MAC addresses e.g. 0:1:2:d:e:f
104 # Add leading zero to each octet where needed.
105 def parse_ether_line(self, words, current_if, ips):
106 macaddress = ''
107 for octet in words[1].split(':'):
108 octet = ('0' + octet)[-2:None]
109 macaddress += (octet + ':')
110 current_if['macaddress'] = macaddress[0:-1]
111
112
113 class SunOSNetworkCollector(NetworkCollector):
114 _fact_class = SunOSNetwork
115 _platform = 'SunOS'