
    Vh\                     H   d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZmZmZmZmZmZ d dlmZ d dlmZ  G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z eeeee      Z eej=                               Z e        Z!d Z"e#dk(  r e"        yy)    )absolute_importdivisionprint_functionaM  
---
module: facts
author: "Petr Klima (@qaxi)"
short_description: Collect facts from remote devices running Cisco SMB
description:
  - Collects a base set of device facts from a remote device that
    is running Cisco SMB.  This module prepends all of the
    base network fact keys with C(ansible_net_<fact>).  The facts
    module will always collect a base set of facts from the device
    and can enable or disable collection of additional facts.
options:
  gather_subset:
    description:
      - When supplied, this argument will restrict the facts collected
        to a given subset.  Possible values for this argument include
        C(all), C(hardware), C(config) and C(interfaces).  Can specify a list of
        values to include a larger subset.  Values can also be used
        with an initial C(!) to specify that a specific subset should
        not be collected.
    required: false
    type: list
    elements: str
    choices: [ 'default', 'all', 'hardware', 'config', 'interfaces', '!hardware', '!config', '!interfaces' ]
    default: '!config'
notes:
  - Supports C(check_mode).
a9  
- name: Collect all facts from the device
  community.ciscosmb.facts:
    gather_subset: all

- name: Collect only the config and default facts
  community.ciscosmb.facts:
    gather_subset:
      - config

- name: Do not collect hardware facts
  community.ciscosmb.facts:
    gather_subset:
      - "!hardware"
aj	  
ansible_net_gather_subset:
  description: The list of fact subsets collected from the device.
  returned: always
  type: list

# default
ansible_net_model:
  description: The model name returned from the device.
  returned: always
  type: str
ansible_net_serialnum:
  description: The serial number of the remote device.
  returned: always
  type: str
ansible_net_version:
  description: The operating system version running on the remote device.
  returned: always
  type: str
ansible_net_hostname:
  description: The configured hostname of the device.
  returned: always
  type: str
ansible_net_uptime:
  description: The uptime of the device.
  returned: always
  type: str
ansible_net_cpu_load:
  description: Current CPU load.
  returned: always
  type: str
ansible_net_stacked_models:
  description: The model names of each device in the stack.
  returned: when multiple devices are configured in a stack
  type: list
ansible_net_stacked_serialnums:
  description: The serial numbers of each device in the stack.
  returned: when multiple devices are configured in a stack
  type: list

# hardware
ansible_net_spacefree_mb:
  description: The available disk space on the remote device in MiB.
  returned: when hardware is configured
  type: dict
ansible_net_spacetotal_mb:
  description: The total disk space on the remote device in MiB.
  returned: when hardware is configured
  type: dict
ansible_net_memfree_mb:
  description: The available free memory on the remote device in MiB.
  returned: when hardware is configured
  type: int
ansible_net_memtotal_mb:
  description: The total memory on the remote device in MiB.
  returned: when hardware is configured
  type: int

# config
ansible_net_config:
  description: The current active config from the device.
  returned: when config is configured
  type: str

# interfaces
ansible_net_all_ipv4_addresses:
  description: All IPv4 addresses configured on the device.
  returned: when interfaces is configured
  type: list
ansible_net_all_ipv6_addresses:
  description: All IPv6 addresses configured on the device.
  returned: when interfaces is configured
  type: list
ansible_net_interfaces:
  description: A hash of all interfaces running on the system.
  returned: when interfaces is configured
  type: dict
ansible_net_neighbors:
  description: The list of neighbors from the remote device.
  returned: when interfaces is configured
  type: dict

N)run_commandsciscosmb_argument_specinterface_canonical_nameciscosmb_split_to_tablesciscosmb_parse_tableciscosmb_merge_dicts)AnsibleModule)	iteritemsc                   ,    e Zd Z e       Zd Zd Zd Zy)	FactsBasec                 >    || _         t               | _        d | _        y )N)moduledictfacts	responses)selfr   s     l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/ciscosmb/plugins/modules/facts.py__init__zFactsBase.__init__   s    V
    c                 R    t        | j                  | j                  d      | _        y NF)commandscheck_rc)r   r   COMMANDSr   )r   s    r   populatezFactsBase.populate   s    %KK$--%
r   c                 2    t        | j                  |d      S r   )r   r   )r   cmds     r   runzFactsBase.run   s    DKK#FFr   N)__name__
__module____qualname__listr   r   r   r!    r   r   r   r      s    vH


Gr   r   c                   h     e Zd Zg dZ fdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Z xZS )Default)zshow versionzshow systemzshow cpu utilizationzshow inventoryc                 p   t         t        |           | j                  d   }|r<| j	                  |      | j
                  d<   | j                  |      | j
                  d<   | j                  d   }|r<| j                  |      | j
                  d<   | j                  |      | j
                  d<   | j                  d   }|r| j                  |      | j
                  d<   | j                  d	   }|r| j                  |      }| j                  |      }t        |      dk\  r/| j                  |      }|| j
                  d
<   || j
                  d<   | j                  |      | j
                  d<   | j                  |      | j
                  d<   | j!                  |      | j
                  d<   || j
                  d<   y y )Nr   versionboot_version   uptimehostname   cpu_load   stacked_modelsstacked_serialnumsmodel	serialnum
hw_version
hw_modules)superr(   r   r   parse_versionr   parse_boot_versionparse_uptimeparse_hostnameparse_cpu_loadparse_inventoryparse_stacked_modelslenparse_stacked_serialnumsparse_modelparse_serialnumparse_hw_version)r   datamodulesr2   r3   	__class__s        r   r   zDefault.populate   s   gt%'~~a $($6$6t$<DJJy!)-)@)@)FDJJ~&~~a #'#4#4T#:DJJx %)%8%8%>DJJz"~~a %)%8%8%>DJJz"~~a **40G!66w?N>"a'%)%B%B7%K"/=

+,3E

/0"&"2"27";DJJw&*&:&:7&CDJJ{#'+'<'<W'EDJJ|$'.DJJ|$ r   c                     t        j                  d|t         j                        }|r|j                  d      S t        j                  d|t         j                        }|r|j                  d      S y )Nz^SW version\s*(\S+)\s*.*$r,   z^  Version:\s*(\S+)\s*.*$researchMgroupr   rE   matchs      r   r9   zDefault.parse_version   sW    		6bddC;;q>!		6bddC;;q>! r   c                 t    t        j                  d|t         j                        }|r|j                  d      S y )NzBoot version\s*(\S+)\s*.*$r,   rI   rN   s      r   r:   zDefault.parse_boot_version   s/    		7rttD;;q>! r   c                 2   t        j                  d|t         j                        }|rp|j                  d      j	                  d      \  }}}|j	                  d      \  }}t        |      dz  t        |      dz  z   t        |      dz  z   t        |      z   S y )Nz ^System Up Time \S+:\s+(\S+)\s*$r,   :,iQ i  <   )rJ   rK   rL   rM   splitint)r   rE   rO   dayhourminssecdayhours           r   r;   zDefault.parse_uptime   s    		=tRTTJ#(;;q>#7#7#< WdC!--,KS$Hu$TT)9:c$i"nMPSTWPXXX	 r   c                 t    t        j                  d|t         j                        }|r|j                  d      S y )Nz^System Name:\s*(\S+)\s*$r,   rI   rN   s      r   r<   zDefault.parse_hostname   s/    		6bddC;;q>! r   c                 t    t        j                  d|t         j                        }|r|j                  d      S y )Nzone minute:\s+(\d+)%;\s*r,   rI   rN   s      r   r=   zDefault.parse_cpu_load   s/    		5tRTTB;;q>! r   c                    t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }|j                         }i }|D ]  }t        j                  dd|      }t        j                  d	d
|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  d|      }|j	                         }|||d   <    |r|S y )Nz\nPIDz  PIDz^\n z\n\nz\n\s*\nz\n"z\s+ z:\sz	\s+DESCR"z"DESCR"z\s+PID"z"PID"z\s+VID"z"VID"z\s+SN"z"SN"z\s*$zd^NAME"(?P<name>[^"]+)"DESCR"(?P<descr>[^"]+)"PID"(?P<pid>[^"]+)"VID"(?P<vid>[^"]+)"SN"(?P<sn>\S+)\s*name)rJ   sub
splitlinesrK   	groupdict)r   rE   linesrF   linerO   moduls          r   r>   zDefault.parse_inventory   s9   vvh.vvfb$'vvgr4(vvj%.! 	+D66$T*D66&$-D66&$-D66,
D9D66*h5D66*h5D66)Wd3D66'3-DIIwE
 OO%E%*GE&M"%	+( N r   c                     g }t        dd      D ]0  }t        |      |v s|j                  |t        |         d          2 |S )Nr,   	   pidrangestrappend)r   rE   modelsns       r   r?   zDefault.parse_stacked_models  sI     q! 	3A1v~d3q6l512	3 r   c                     g }t        dd      D ]0  }t        |      |v s|j                  |t        |         d          2 |S )Nr,   rj   snrl   )r   rE   rs   rq   s       r   rA   z Default.parse_stacked_serialnums!  sI     q! 	.A1v~		$s1v,t,-	. 	r   c                 j    |d   d   }d| j                   v rt        j                  dd|      }d|z   }|S )N1rk   r2   z-.*$r_   zStack )r   rJ   rc   )r   rE   r4   s      r   rB   zDefault.parse_model+  s=    S	% tzz)FF7B.Eu$Er   c                     |d   d   }|S )Nru   rs   r&   r   rE   rs   s      r   rC   zDefault.parse_serialnum3  s    #Yt_	r   c                     |d   d   }|S )Nru   vidr&   rw   s      r   rD   zDefault.parse_hw_version8  s    #Yu	r   )r"   r#   r$   r   r   r9   r:   r;   r<   r=   r>   r?   rA   rB   rC   rD   __classcell__rG   s   @r   r(   r(      sH    H/>""Y"" D
r   r(   c                   *     e Zd ZdgZ fdZd Z xZS )Hardwaredirc                 p    t         t        |           | j                  d   }|r| j	                  |       y y )Nr   )r8   r}   r   r   parse_filesystem_infor   rE   rG   s     r   r   zHardware.populateD  s4    h&(~~a &&t, r   c                    t        j                  d|t         j                        }|rt        t	        |j                  d            dz  dz  d      | j                  d<   t        j                  d|t         j                        }t        t	        |j                  d            dz  dz  d      | j                  d<   y t        j                  d|t         j                        }|rit        t	        |j                  d            dz  d      | j                  d<   t        t	        |j                  d            dz  d      | j                  d<   y y )	Nz Total size of (\S+): (\d+) bytesr/   i   r,   spacetotal_mbzFree size of (\S+): (\d+) bytesspacefree_mbz(\d+)K of (\d+)K are free)rJ   rK   rL   roundrV   rM   r   rN   s      r   r   zHardware.parse_filesystem_infoJ  s    		=tRTTJ*/EKKN0Cd0JT0QST*UDJJ'II@$ME).s5;;q>/BT/ID/PRS)TDJJ~& II:D"$$GE.3CA4G$4NPQ.R

?+-23u{{1~3F3Mq-Q

>* r   )r"   r#   r$   r   r   r   rz   r{   s   @r   r}   r}   >  s     	H-Rr   r}   c                   $     e Zd ZdgZ fdZ xZS )Configzshow running-config detailedc                 l    t         t        |           | j                  d   }|r|| j                  d<   y y )Nr   config)r8   r   r   r   r   r   s     r   r   zConfig.populate]  s4    fd$&~~a #'DJJx  r   )r"   r#   r$   r   r   rz   r{   s   @r   r   r   Y  s    ./H( (r   r   c                        e Zd Zg dZ ej
                  d      Z ej
                  d      Z fdZd Z	d Z
d Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )
Interfaces)zshow ports jumbo-framezshow ip interfacezshow ipv6 interface briefzshow interfaces statuszshow interfaces configurationzshow interfaces descriptionzshow lldp neighborszC([\w\d\-]+)=\"?(\w{3}/\d{2}/\d{4}\s\d{2}:\d{2}:\d{2}|[\w\d\-\.:/]+)z
^\s+(?!\d)c                    t         t        |           t               | j                  d<   t               | j                  d<   t               | j                  d<   t               | j                  d<   | j                  d   }|r| j                  |       | j                  d   }|r| j                  |       | j                  d   }|r| j                  |       | j                  d   }|r| j                  |       | j                  d	   }|r| j                  |       | j                  d
   }|r| j                  |       | j                  d   }|r| j                  |       y y )N
interfacesall_ipv4_addressesall_ipv6_addresses	neighborsr   r,   r/   r1            )r8   r   r   r   r   r%   r   populate_interfaces_mtupopulate_addresses_ipv4populate_addresses_ipv6populate_interfaces_status!populate_interfaces_configurationpopulate_interfaces_descriptionpopulate_neighborsr   s     r   r   zInterfaces.populateu  s0   j$(*#'6

< +/6

'(+/6

'("&&

;~~a ((.~~a ((.~~a ((.~~a ++D1~~a 2248~~a 006~~a ##D) r   c                 0   t               }|D ]  }||   }t               }|d   j                         |d<   |d   |d<   | j                  |d<   |d   j                         |d<   |d   j                         |d	<   |d
   j                         |d<   |d   j                         |d<   |d   j                         |d<   |d   dk(  rt        |d         dz  |d<   |d   |d<   nd |d<   |d   |d<   |D ]  }||   dk(  sd ||<    ||t	        |d         <    |S )Nr   stater,   typemtur/   duplexr   negotiationr   control   presure   modeUpr1     	bandwidthbandwith--r   r   lower_mturV   r   r   interface_tabler   keyi	interfaces         r   %_populate_interfaces_status_interfacez0Interfaces._populate_interfaces_status_interface  sL   V
" 	CC$AI!"1Ig !!If#yyIe"#A$**,Ih'(tzz|Im$#$Q4::<Ii #$Q4::<Ii  !!

Iftt|),QqTT)9	+&(1+(>	*%)-	+&(1+(>	*%  *S>T)%)IcN* :CJ/!561	C2 r   c                    t               }|D ]  }t               }||   }|d   j                         |d<   |d   |d<   | j                  |d<   |d   j                         |d<   |d   j                         |d	<   |d
   j                         |d<   |d   dk(  rt        |d         dz  |d<   |d   |d<   nd |d<   |d   |d<   |D ]  }||   dk(  sd ||<    ||t	        |d         <    |S )Nr   r   r,   r   r   r/   r   r   r   r   r   r   r1   r   r   r   r   r   r   r   r   r   r   r   r   s         r   &_populate_interfaces_status_portchanelz1Interfaces._populate_interfaces_status_portchanel  s$   V
" 	CCI$A!"1Ig !!If#yyIe"#A$**,Ih'(tzz|Im$#$Q4::<Ii tt|),QqTT)9	+&(1+(>	*%)-	+&(1+(>	*%  *S>T)%)IcN* :CJ/!56-	C0 r   c                 .   t        |      }t        |d         }t        |d         }| j                  |      }t        | j                  d   |      | j                  d<   | j                  |      }t        | j                  d   |      | j                  d<   y Nr   r,   r   )r	   r
   r   r   r   r   r   rE   tablesr   portchanel_tabler   s         r   r   z%Interfaces.populate_interfaces_status  s    )$/.vay9/q	:??P
#7JJ|$j$


<  @@AQR
#7JJ|$j$


< r   c                     t               }|D ]N  }||   }t               }|d   j                         |d<   |d   j                         |d<   ||t        |d         <   P |S )Nr   admin_stater   mdixr   r   r   r   r   s         r   ,_populate_interfaces_configuration_interfacez7Interfaces._populate_interfaces_configuration_interface  so    V
" 	CC$AI'(tzz|Im$ !!

If9BJ/!56	C r   c                     t               }|D ]8  }t               }||   }|d   j                         |d<   ||t        |d         <   : |S )Nr   r   r   r   r   s         r   -_populate_interfaces_configuration_portchanelz8Interfaces._populate_interfaces_configuration_portchanel  s[    V
" 	CCI$A'(tzz|Im$9BJ/!56	C r   c                 .   t        |      }t        |d         }t        |d         }| j                  |      }t        | j                  d   |      | j                  d<   | j                  |      }t        | j                  d   |      | j                  d<   y r   )r	   r
   r   r   r   r   r   s         r   r   z,Interfaces.populate_interfaces_configuration  s    )$/.vay9/q	:FFW
#7JJ|$j$


<  GG

 $8JJ|$j$


< r   c                     t               }|D ]7  }||   }t               }|d   |d<   |d   dk(  rd |d<   ||t        |d         <   9 |S Nr,   descriptionr_   r   r   r   r   s         r   *_populate_interfaces_description_interfacez5Interfaces._populate_interfaces_description_interface  sl    V
" 		CC$AI'(tIm$'2-+/	-(9BJ/!56		C r   c                     t               }|D ]7  }t               }||   }|d   |d<   |d   dk(  rd |d<   ||t        |d         <   9 |S r   r   r   s         r   +_populate_interfaces_description_portchanelz6Interfaces._populate_interfaces_description_portchanel  sl    V
" 
	CCI$A'(tIm$'2-+/	-(9BJ/!56
	C r   c                 2   t        |      }t        |d   d      }t        |d   d      }| j                  |      }t        | j                  d   |      | j                  d<   | j                  |      }t        | j                  d   |      | j                  d<   y )Nr   Fr,   r   )r	   r
   r   r   r   r   r   s         r   r   z*Interfaces.populate_interfaces_description/  s    )$/.vay%@/q	5ADD_U
#7JJ|$j$


<  EEFVW
#7JJ|$j$


< r   c                    t               }|D ]  }||   d   }t        ||   d         }|j                  d      \  }}|j                  |       | j	                  |       d| j
                  d   |   vrt               | j
                  d   |   d<   | j
                  d   |   d   j                  t        ||              |S )Nr   r,   /ipv4r   )addresssubnet)r%   r   rU   ro   _new_interfacer   r   )r   ip_tableipsr   cidrr   ipmasks           r   _populate_address_ipv4z!Interfaces._populate_address_ipv4>  s    f 	CC=#D0#q1ABIzz#HBJJrN 	*TZZ5i@@>Bf

<(3F;JJ|$Y/7>>R-	" 
r   c                 v    t        |      }t        |d         }| j                  |      }|| j                  d<   y )Nr   r   )r	   r
   r   r   r   rE   r   r   r   s        r   r   z"Interfaces.populate_addresses_ipv4T  s9    )$/'q	2))(3+.

'(r   c                 \   t               }|D ]  }||   d   }t        ||   d         }|j                  |       | j                  |       d| j                  d   |   vrt               | j                  d   |   d<   | j                  d   |   d   j                  t        |              |S )Nr1   r   ipv6r   )r   )r%   r   ro   r   r   r   )r   r   r   r   r   r   s         r   _populate_address_ipv6z!Interfaces._populate_address_ipv6[  s    f 	QC#q!B0#q1ABIJJrN 	*TZZ5i@@>Bf

<(3F;JJ|$Y/7>>tB?OP	Q 
r   c                    || j                   d   v ry t               | j                   d   |<   | j                  | j                   d   |   d<   d| j                   d   |   d<   d | j                   d   |   d<   d| j                   d   |   d<   d | j                   d   |   d<   d | j                   d   |   d<   d | j                   d   |   d	<   d | j                   d   |   d
<   d | j                   d   |   d<   y )Nr   r   upr   r   r   r   r   r   r   r   )r   r   r   )r   r   s     r   r   zInterfaces._new_interfacem  s
   

<0026&DJJ|$Y/9=DJJ|$Y/6AEDJJ|$Y/>AEDJJ|$Y/>;?DJJ|$Y/8?CDJJ|$Y/<>BDJJ|$Y/
;<@DJJ|$Y/9AEDJJ|$Y/>=ADJJ|$Y/	:r   c                 v    t        |      }t        |d         }| j                  |      }|| j                  d<   y )Nr   r   )r	   r
   r   r   r   s        r   r   z"Interfaces.populate_addresses_ipv6~  s9    )$/'q	2))(3+.

'(r   c                 x    t        j                  d|t         j                        }|r
d}|| _        y d}|| _        y )NzJumbo frames are enabledi(#  i  )rJ   rK   rL   r   )r   rE   rO   r   s       r   r   z"Interfaces.populate_interfaces_mtu  s8    		5tRTTBC 	 C	r   c                    t        |      }t        |d   ddg      }t               }|D ]I  }||   }t        |d         }|d   }|d   }	||	d}
||vrt	               ||<   ||   j                  |
       K || j                  d<   y )Nr   r1   r   )allow_empty_fieldsr/   )hostportr   )r	   r
   r   r   r%   ro   r   )r   rE   r   neighbor_tabler   r   neighborifcnamer   r   hostports              r   r   zInterfaces.populate_neighbors  s    )$/-1I !1v

 F	! 	0C%c*H.x{;GA;DA;D $d3Hi'%)V	'"g%%h/	0 #,

;r   )r"   r#   r$   r   rJ   compile	DETAIL_REWRAPPED_LINE_REr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rz   r{   s   @r   r   r   d  s    H 

NI !bjj/O"*H<:

""
,/$"/,r   r   )defaulthardwarer   r   c            	         t        t        dgddg d            } | j                  t               t        | d      }|j                  d	   }t               }t               }|D ]  }|d
k(  r|j                  t               |j                  d      r#|dd }|d
k(  r|j                  t               Od}nd}|t        vr|j                  d|z         |r|j                  |       |j                  |        |s|j                  t               |j                  |       |j                  d       t               }t        |      |d	<   t               }|D ]  }	|j                  t        |	   |             ! |D ]-  }
|
j                          |j                  |
j                         / t               }t!        |      D ]  \  }	}d|	z  }	|||	<    |j#                  |t$               y)z%main entry point for module execution!configr%   rn   )allr   r   r   r   z	!hardwarez!interfacesr   )r   r   elementschoices)gather_subsetT)argument_specsupports_check_moder   r   !r,   NFzBad subset: %s)msgr   zansible_net_%s)ansible_factswarnings)r   updater   r   paramssetVALID_SUBSETS
startswith	fail_jsonadddifference_updater%   ro   FACT_SUBSETSr   r   r   	exit_jsonr   )r   r   r   runable_subsetsexclude_subsetssubsetexcluder   	instancesr   instr   values                r   mainr    s   K		
M$ /0DQFMM/2MeOeO (U?""=1S!ABZF&&}5GG&!1F!:;'')(, }-%%o6	"FE!/2E/I 4c*6234  !TZZ ! FM& #
U$"c# =8Dr   __main__)$
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNrJ   Dansible_collections.community.ciscosmb.plugins.module_utils.ciscosmbr   r   r   r	   r
   r   ansible.module_utils.basicr   ansible.module_utils.sixr   objectr   r(   r}   r   r   r   r  	frozensetkeysr   r%   r   r  r"   r&   r   r   <module>r     s   
 A @: R
f 
  5 .G G$Ri RjRy R6(Y (C, C,L
 	 ,++-.6IEX zF r   