Debian Codegraph @

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

raw
1 # iSCSI initiator related facts collection for Ansible.
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20 import sys
21 import typing as t
22
23 from ansible.module_utils.facts.utils import get_file_content
24 from ansible.module_utils.facts.network.base import NetworkCollector
25
26
27 class IscsiInitiatorNetworkCollector(NetworkCollector):
28 name = 'iscsi'
29 _fact_ids = set() # type: t.Set[str]
30
31 def collect(self, module=None, collected_facts=None):
32 """
33 Example of contents of /etc/iscsi/initiatorname.iscsi:
34
35 ## DO NOT EDIT OR REMOVE THIS FILE!
36 ## If you remove this file, the iSCSI daemon will not start.
37 ## If you change the InitiatorName, existing access control lists
38 ## may reject this initiator. The InitiatorName must be unique
39 ## for each iSCSI initiator. Do NOT duplicate iSCSI InitiatorNames.
40 InitiatorName=iqn.1993-08.org.debian:01:44a42c8ddb8b
41
42 Example of output from the AIX lsattr command:
43
44 # lsattr -E -l iscsi0
45 disc_filename /etc/iscsi/targets Configuration file False
46 disc_policy file Discovery Policy True
47 initiator_name iqn.localhost.hostid.7f000002 iSCSI Initiator Name True
48 isns_srvnames auto iSNS Servers IP Addresses True
49 isns_srvports iSNS Servers Port Numbers True
50 max_targets 16 Maximum Targets Allowed True
51 num_cmd_elems 200 Maximum number of commands to queue to driver True
52
53 Example of output from the HP-UX iscsiutil command:
54
55 #iscsiutil -l
56 Initiator Name : iqn.1986-03.com.hp:mcel_VMhost3.1f355cf6-e2db-11e0-a999-b44c0aef5537
57 Initiator Alias :
58
59 Authentication Method : None
60 CHAP Method : CHAP_UNI
61 Initiator CHAP Name :
62 CHAP Secret :
63 NAS Hostname :
64 NAS Secret :
65 Radius Server Hostname :
66 Header Digest : None, CRC32C (default)
67 Data Digest : None, CRC32C (default)
68 SLP Scope list for iSLPD :
69 """
70
71 iscsi_facts = {}
72 iscsi_facts['iscsi_iqn'] = ""
73 if sys.platform.startswith('linux') or sys.platform.startswith('sunos'):
74 for line in get_file_content('/etc/iscsi/initiatorname.iscsi', '').splitlines():
75 if line.startswith('#') or line.startswith(';') or line.strip() == '':
76 continue
77 if line.startswith('InitiatorName='):
78 iscsi_facts['iscsi_iqn'] = line.split('=', 1)[1]
79 break
80 elif sys.platform.startswith('aix'):
81 cmd = module.get_bin_path('lsattr')
82 if cmd is None:
83 return iscsi_facts
84
85 cmd += " -E -l iscsi0"
86 rc, out, err = module.run_command(cmd)
87 if rc == 0 and out:
88 line = self.findstr(out, 'initiator_name')
89 iscsi_facts['iscsi_iqn'] = line.split()[1].rstrip()
90
91 elif sys.platform.startswith('hp-ux'):
92 cmd = module.get_bin_path(
93 'iscsiutil',
94 opt_dirs=['/opt/iscsi/bin']
95 )
96 if cmd is None:
97 return iscsi_facts
98
99 cmd += " -l"
100 rc, out, err = module.run_command(cmd)
101 if out:
102 line = self.findstr(out, 'Initiator Name')
103 iscsi_facts['iscsi_iqn'] = line.split(":", 1)[1].rstrip()
104
105 return iscsi_facts
106
107 def findstr(self, text, match):
108 for line in text.splitlines():
109 if match in line:
110 found = line
111 return found