
    VhWA                         d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlmZ dZdad ZddZd	 Zdd
ZddZddZd Zd ZddZddZddZd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona\
  
module: open_iscsi
author:
  - Serge van Ginderachter (@srvg)
short_description: Manage iSCSI targets with Open-iSCSI
description:
  - Discover targets on given portal, (dis)connect targets, mark targets to manually or auto start, return device nodes of
    connected targets.
requirements:
  - open_iscsi library and tools (iscsiadm)
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  portal:
    description:
      - The domain name or IP address of the iSCSI target.
    type: str
    aliases: [ip]
  port:
    description:
      - The port on which the iSCSI target process listens.
    type: str
    default: '3260'
  target:
    description:
      - The iSCSI target name.
    type: str
    aliases: [name, targetname]
  login:
    description:
      - Whether the target node should be connected.
      - When O(target) is omitted, will login to all available.
    type: bool
    aliases: [state]
  node_auth:
    description:
      - The value for C(node.session.auth.authmethod).
    type: str
    default: CHAP
  node_user:
    description:
      - The value for C(node.session.auth.username).
    type: str
  node_pass:
    description:
      - The value for C(node.session.auth.password).
    type: str
  node_user_in:
    description:
      - The value for C(node.session.auth.username_in).
    type: str
    version_added: 3.8.0
  node_pass_in:
    description:
      - The value for C(node.session.auth.password_in).
    type: str
    version_added: 3.8.0
  auto_node_startup:
    description:
      - Whether the target node should be automatically connected at startup.
    type: bool
    aliases: [automatic]
  auto_portal_startup:
    description:
      - Whether the target node portal should be automatically connected at startup.
    type: bool
    version_added: 3.2.0
  discover:
    description:
      - Whether the list of target nodes on the portal should be (re)discovered and added to the persistent iSCSI database.
      - Keep in mind that C(iscsiadm) discovery resets configuration, like C(node.startup) to manual, hence combined with
        O(auto_node_startup=true) will always return a changed state.
    type: bool
    default: false
  show_nodes:
    description:
      - Whether the list of nodes in the persistent iSCSI database should be returned by the module.
    type: bool
    default: false
  rescan:
    description:
      - Rescan an established session for discovering new targets.
      - When O(target) is omitted, will rescan all sessions.
    type: bool
    default: false
    version_added: 4.1.0
aB  
- name: Perform a discovery on sun.com and show available target nodes
  community.general.open_iscsi:
    show_nodes: true
    discover: true
    portal: sun.com

- name: Perform a discovery on 10.1.2.3 and show available target nodes
  community.general.open_iscsi:
    show_nodes: true
    discover: true
    ip: 10.1.2.3

- name: Discover targets on portal and login to the ones available
  community.general.open_iscsi:
    portal: '{{ iscsi_target }}'
    login: true
    discover: true

- name: Connect to the named target, after updating the local persistent database (cache)
  community.general.open_iscsi:
    login: true
    target: iqn.1986-03.com.sun:02:f8c1f9e0-c3ec-ec84-c9c9-8bfb0cd5de3d

- name: Disconnect from the cached named target
  community.general.open_iscsi:
    login: false
    target: iqn.1986-03.com.sun:02:f8c1f9e0-c3ec-ec84-c9c9-8bfb0cd5de3d

- name: Override and disable automatic portal login on specific portal
  community.general.open_iscsi:
    login: false
    portal: 10.1.1.250
    auto_portal_startup: false
    target: iqn.1986-03.com.sun:02:f8c1f9e0-c3ec-ec84-c9c9-8bfb0cd5de3d

- name: Rescan one or all established sessions to discover new targets (omit target for all sessions)
  community.general.open_iscsi:
    rescan: true
    target: iqn.1986-03.com.sun:02:f8c1f9e0-c3ec-ec84-c9c9-8bfb0cd5de3d
N)AnsibleModuleiscsiadmc                 L    | j                          |j                          | |k(  S N)sort)l1l2s     p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/open_iscsi.pycompare_nodelistsr      s    GGIGGI8O    c                    t         ddg}| j                  |      \  }}}g }|dk(  r|j                         }|D ]j  }|j                         }	t	        |	      dkD  r| j                  d|       |	d   }
|	d   j                  d      }	|	d   }|||k(  sZ|j                  |
       l |S |d	k(  s	|d
k(  rd|v r	 |S | j                  |||       |S )N--modenoder      zerror parsing output)msgcmd   :      zo records foundr   rcr   )iscsiadm_cmdrun_command
splitlinessplitlen	fail_jsonappend)moduleportalr   r   outerrnodeslineslinepartstargettarget_portals               r   iscsi_get_cached_nodesr-      s    6
*C%%c*LBSE	Qw  
	%DJJLE5zA~  %; E1XF!HNN3'E!!HM~=!8V$
	%( L 
rbCi$5$< L 	SRS1Lr   c           	      N    t         ddddd|d|g}| j                  |d       y )	Nr   	discoveryz--typesendtargets--portalr   Tcheck_rcr   r   )r#   r$   portr   s       r   iscsi_discoverr6      s0    ;-`fhlUm
nC
sT*r   c                 b    |t         dddg}nt         dddd|g}| j                  |      \  }}}|S )Nr   sessionz--rescanr   z-Tr4   )r#   r+   r   r   r%   r&   s         r   iscsi_rescanr9      sB    ~Xy*=Xvz4H%%c*LBSJr   c                    t         ddg}| j                  |      \  }}}|d}|d}|dk(  rHt        j                  |      d|dt        j                  |      }t        j                  ||      d uS |dk(  ry| j                  |||	       y )
Nr   r8    r   r   z.*r   Fr   )r   r   reescapesearchr!   )	r#   r+   r$   r5   r   r   r%   r&   	search_res	            r   target_loggedonr@      s    9
-C%%c*LBS~|	Qw#%99V#4dBIIf<MN	yyC(44	rSRS1r   c                    | j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }	|r8d|fd|fd|fg}
|
D ](  \  }}t        d	d
d|dd|d|g
}| j                  ||       * |r6d|fd|	fg}
|
D ])  \  }}t        d|d|d|}| j                  ||       + t        d	d
d|dg}|)|'|j                  d       |j                  |d|       | j                  ||      \  }}}|S )N	node_auth	node_user	node_passnode_user_innode_pass_inznode.session.auth.authmethodznode.session.auth.usernameznode.session.auth.passwordr   r   --targetname--op=update--name--valuer2   znode.session.auth.username_inznode.session.auth.password_inz --mode node --targetname z --op=update --name z	 --value z--loginr1   r   )paramsr   r   r"   )r#   r+   r3   r$   r5   rB   rC   rD   rE   rF   rK   namevaluer   r   r%   r&   s                    r   target_loginrN      s`   k*Ik*Ik*I==0L==0L19=/;/;= $ 	7MT56>6=Zbdhjsuz{CsX6	7 2LA2LAC# 	7MT5WcekmqsxyCsX6	7 6>69
MCd.

:

fd+,%%cH%=LBSIr   c                 B    t         ddd|dg}| j                  |d       y )Nr   r   rG   z--logoutTr2   r4   )r#   r+   r   s      r   target_logoutrP     s'    6>6:
NC
sT*r   c                     t        j                   d| z        }g }|D ]<  }d|vst        j                  j                  |      }||vs,|j	                  |       > |S )Nz/dev/disk/by-path/*%s*z-part)globospathrealpathr"   )r+   devicesdevdisksdevdevdisks        r   target_device_noderZ     sc     ii069:GH )#gg&&s+Gh&() Or   c                     t         ddd|g}|)|'|j                  d       |j                  |d|       | j                  |d      \  }}}|j                         }|D ]  }d|v sd	|v c S  y
)Nr   r   rG   r1   r   Tr2   node.startup	automaticF)r   r"   r   r   )	r#   r+   r$   r5   r   dummyr%   r(   r)   s	            r   target_isautor_     s    6>6
BCd.

:

fd+,**3*>E3NNE 'T!$&&' r   c           
          t         ddd|dddddg
}|)|'|j                  d	       |j                  |d
|       | j                  |d       y )Nr   r   rG   rH   rI   r\   rJ   r]   r1   r   Tr2   r   r"   r   r#   r+   r$   r5   r   s        r   target_setautorc   &  sb    6>6=RZ\jlu  xC  DCd.

:

fd+,
sT*r   c           
          t         ddd|dddddg
}|)|'|j                  d	       |j                  |d
|       | j                  |d       y )Nr   r   rG   rH   rI   r\   rJ   manualr1   r   Tr2   ra   rb   s        r   target_setmanualrf   0  s^    6>6=RZ\jluw  ACd.

:

fd+,
sT*r   c                  	   t        t        t        ddg      t        dd      t        dddg      t        dd      t        d	      t        dd
      t        d	      t        dd
      t        ddg      t        ddg      t        d	      t        dd      t        dd      t        dd            ddgddggdd
dgfdd
dgfdd
dgfgd
      } | j                  dd
      a| j                  d   }|r 	 t        j                  |d       d   d   d   }| j                  d   }| j                  d!   }| j                  d"   }| j                  d   }| j                  d   }| j                  d   }| j                  d#   }| j                  d$   }	| j                  }
t        | |      }d%di}d}d
}|r=|
r|}nt        | ||       t        | |      }t        ||      s|d%xx   d
z  cc<   d
|d&<   n|}||;|t        |      d'kD  r+d
}d}n&d}|D ]  }||k(  s	d
} n |s| j                  d(        |r||d)<   |}|rg |d*<   |D ]  }t        | |||      }|r|s|s)|s'|d%xx   dz  cc<   |s)|d*xx   t        |      z  cc<   @|
s|r;t!        | ||||      }t#        j$                  d'       |d*xx   t        |      z  cc<   nt'        | |       t        |      d'kD  rd+k(  r|d%xx   dz  cc<   d|d,<   |d%xx   d
z  cc<   d
|d,<   |d%xx   d
z  cc<   d
|d,<    nt        | |||      }|r|s|s |s|d%xx   dz  cc<   |rvt        |      |d*<   ng|
sS|r2t!        | |||       t#        j$                  d'       t        |      |d*<   nt'        | |       |d%xx   d
z  cc<   d
|d,<   n|d%xx   d
z  cc<   d
|d,<   |k|sit)        | |      }|r|s|s|s|d%xx   dz  cc<   d|d-<   nB|
s.|rt+        | |       nt-        | |       |d%xx   d
z  cc<   d
|d-<   n|d%xx   d
z  cc<   d
|d-<   |q|sot)        | |||      }|r|s|s|s|d%xx   dz  cc<   d|d.<   nF|
s2|rt+        | |||       nt-        | |||       |d%xx   d
z  cc<   d
|d.<   n|d%xx   d
z  cc<   d
|d.<   |	durd
|d%<   t/        | |      |d/<    | j0                  d0i | y # t
        j                  $ r | j                  d        Y w xY w)1Nstrip)typealiases3260)rj   defaultrL   
targetnameCHAP)rj   T)rj   no_logboolstater]   F)r$   r5   r+   rB   rC   rD   rE   rF   loginauto_node_startupauto_portal_startupdiscover
show_nodesrescanrC   rD   rE   rF   rv   r$   rt   r+   ru   )argument_specrequired_togetherrequired_ifsupports_check_moder   )requiredr      zPortal address is incorrect)r   r5   rs   rw   rx   changedcache_updatedr   zSpecified target not foundr'   devicenodes   connection_changedautomatic_changedautomatic_portal_changedsessions )r   dictget_bin_pathr   rK   socketgetaddrinfogaierrorr!   
check_moder-   r6   r   r    r@   rZ   rN   timesleeprP   r_   rc   rf   r9   	exit_json)r#   r$   r+   r5   rs   r]   automatic_portalrv   rw   rx   checkcachedresultlogin_to_all_nodesr3   r'   check_targetr   index_targetloggedonlogin_resultisautos                         r   mainr   :  s    UTF35&1UV\,BCv6&d35)56 FWI6"F $& 1vu57VU3%
* (57WXz* $
3"D8*57 !7F> &&zD&AL ]]8$F	@''5a8;A>F ]]8$F== DMM'"E12I}}%:;}}Z(H|,J]]8$FE#FF3F FHE6640*66:E /9%&*F?#I1>5zA~ &*"  !L 6>#'L    %A Bw$&F=! % 8*6<Nhh9%.%}-1CL1QQ-'3FL(TZ\`'a

1}-1CL1QQ-%fl; 5zA~,"*<y)U2)7<34y)T1)7;349%-%37F/0184 'vvvtDH(E(y!U*!,>v,FF=) >JJqM,>v,FF=)!&&1y!T)!/3+,y!T)!/3+,%7vv.&)F9&*/F&'vv. 09%*.F&'9%*.F&'#,>vvvt<1A&9&16F-.vvvt< >9%15F-.9%15F-.U y)&&9zFvS  	@!>?	@s   =S %S/.S/__main__r	   )NN)
__future__r   r   r   rj   __metaclass__DOCUMENTATIONEXAMPLESrR   rS   r<   r   r   ansible.module_utils.basicr   ISCSIADMr   r   r-   r6   r9   r@   rN   rP   rZ   r_   rc   rf   r   __name__r   r   r   <module>r      s    A @[z(T  	 	   4<+
2$>+
  ++qh zF r   