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 18from __future__ import (absolute_import, division, print_function) 19__metaclass__ = type 20 21import sys 22import subprocess 23 24from ansible.module_utils.common.process import get_bin_path 25from ansible.module_utils.facts.utils import get_file_content 26from ansible.module_utils.facts.network.base import NetworkCollector 27 28 29class IscsiInitiatorNetworkCollector(NetworkCollector): 30 name = 'iscsi' 31 _fact_ids = set() 32 33 def collect(self, module=None, collected_facts=None): 34 """ 35 Example of contents of /etc/iscsi/initiatorname.iscsi: 36 37 ## DO NOT EDIT OR REMOVE THIS FILE! 38 ## If you remove this file, the iSCSI daemon will not start. 39 ## If you change the InitiatorName, existing access control lists 40 ## may reject this initiator. The InitiatorName must be unique 41 ## for each iSCSI initiator. Do NOT duplicate iSCSI InitiatorNames. 42 InitiatorName=iqn.1993-08.org.debian:01:44a42c8ddb8b 43 44 Example of output from the AIX lsattr command: 45 46 # lsattr -E -l iscsi0 47 disc_filename /etc/iscsi/targets Configuration file False 48 disc_policy file Discovery Policy True 49 initiator_name iqn.localhost.hostid.7f000002 iSCSI Initiator Name True 50 isns_srvnames auto iSNS Servers IP Addresses True 51 isns_srvports iSNS Servers Port Numbers True 52 max_targets 16 Maximum Targets Allowed True 53 num_cmd_elems 200 Maximum number of commands to queue to driver True 54 55 Example of output from the HP-UX iscsiutil command: 56 57 #iscsiutil -l 58 Initiator Name : iqn.1986-03.com.hp:mcel_VMhost3.1f355cf6-e2db-11e0-a999-b44c0aef5537 59 Initiator Alias : 60 61 Authentication Method : None 62 CHAP Method : CHAP_UNI 63 Initiator CHAP Name : 64 CHAP Secret : 65 NAS Hostname : 66 NAS Secret : 67 Radius Server Hostname : 68 Header Digest : None, CRC32C (default) 69 Data Digest : None, CRC32C (default) 70 SLP Scope list for iSLPD : 71 """ 72 73 iscsi_facts = {} 74 iscsi_facts['iscsi_iqn'] = "" 75 if sys.platform.startswith('linux') or sys.platform.startswith('sunos'): 76 for line in get_file_content('/etc/iscsi/initiatorname.iscsi', '').splitlines(): 77 if line.startswith('#') or line.startswith(';') or line.strip() == '': 78 continue 79 if line.startswith('InitiatorName='): 80 iscsi_facts['iscsi_iqn'] = line.split('=', 1)[1] 81 break 82 elif sys.platform.startswith('aix'): 83 try: 84 cmd = get_bin_path('lsattr') 85 except ValueError: 86 return iscsi_facts 87 88 cmd += " -E -l iscsi0" 89 rc, out, err = module.run_command(cmd) 90 if rc == 0 and out: 91 line = self.findstr(out, 'initiator_name') 92 iscsi_facts['iscsi_iqn'] = line.split()[1].rstrip() 93 94 elif sys.platform.startswith('hp-ux'): 95 # try to find it in the default PATH and opt_dirs 96 try: 97 cmd = get_bin_path('iscsiutil', opt_dirs=['/opt/iscsi/bin']) 98 except ValueError: 99 return iscsi_facts 100 101 cmd += " -l" 102 rc, out, err = module.run_command(cmd) 103 if out: 104 line = self.findstr(out, 'Initiator Name') 105 iscsi_facts['iscsi_iqn'] = line.split(":", 1)[1].rstrip() 106 107 return iscsi_facts 108 109 def findstr(self, text, match): 110 for line in text.splitlines(): 111 if match in line: 112 found = line 113 return found 114