
    Vhy+                         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 dlmZ  e
j$                         ZdZdZ G d	 d
e      Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: modprobe
short_description: Load or unload kernel modules
author:
  - David Stygstra (@stygstra)
  - Julien Dauphant (@jdauphant)
  - Matt Jeffery (@mattjeffery)
description:
  - Load or unload kernel modules.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    type: str
    required: true
    description:
      - Name of kernel module to manage.
  state:
    type: str
    description:
      - Whether the module should be present or absent.
    choices: [absent, present]
    default: present
  params:
    type: str
    description:
      - Modules parameters.
    default: ''
  persistent:
    type: str
    choices: [disabled, absent, present]
    default: disabled
    version_added: 7.0.0
    description:
      - Persistency between reboots for configured module.
      - This option creates files in C(/etc/modules-load.d/) and C(/etc/modprobe.d/) that make your module configuration persistent
        during reboots.
      - If V(present), adds module name to C(/etc/modules-load.d/) and params to C(/etc/modprobe.d/) so the module will be
        loaded on next reboot.
      - If V(absent), will comment out module name from C(/etc/modules-load.d/) and comment out params from C(/etc/modprobe.d/)
        so the module will not be loaded on next reboot.
      - If V(disabled), will not touch anything and leave C(/etc/modules-load.d/) and C(/etc/modprobe.d/) as it is.
      - Note that it is usually a better idea to rely on the automatic module loading by PCI IDs, USB IDs, DMI IDs or similar
        triggers encoded in the kernel modules themselves instead of configuration like this.
      - In fact, most modern kernel modules are prepared for automatic loading already.
      - B(Note:) This option works only with distributions that use C(systemd) when set to values other than V(disabled).
a  
- name: Add the 802.1q module
  community.general.modprobe:
    name: 8021q
    state: present

- name: Add the dummy module
  community.general.modprobe:
    name: dummy
    state: present
    params: 'numdummies=2'

- name: Add the dummy module and make sure it is loaded after reboots
  community.general.modprobe:
    name: dummy
    state: present
    params: 'numdummies=2'
    persistent: present
N)AnsibleModule)	to_nativez/etc/modules-load.dz/etc/modprobe.dc                       e Zd Zd Zd Zed        Zed        Zed        Zd Z	ed        Z
d Zd	 Zd
 Zd Zd Zed        Zed        Zd Zd Zed        Zy)Modprobec                 B   || _         |j                  dd      | _        |j                  | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        d| _        t        j                  dj                  | j                              | _        t        j                  d	j                  | j                              | _        t        j                  d
j                  | j                              | _        y )NmodprobeTstatenameparams
persistentFz^ *{0} *(?:[#;].*)?\n?\Zz&^options {0} \w+=\S+ *(?:[#;].*)?\n?\Zz(^options {0} (\w+=\S+) *(?:[#;].*)?\n?\Z)moduleget_bin_pathmodprobe_bin
check_moder   desired_stater   r   changedrecompileformatre_find_modulere_find_paramsre_get_params_and_values)selfr   s     n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/modprobe.py__init__zModprobe.__init__d   s    "//
DA ++#]]73MM&)	mmH- --5 jj)D)K)KDII)VW jj)R)Y)YZ^ZcZc)de(*

3^3e3efjfofo3p(q%    c                    | j                   g}| j                  r|j                  d       |j                  | j                  gt        j                  | j                        z          | j                  j                  |      \  }}}|dk7  r+ | j                  j                  d||||d| j                  S | j                  s| j                         rd| _        y | j                  j                  | j                   dd| j                  gt        j                  | j                        z         \  }}}|dk7  r| j                  j                  |       y y )N-nr   msgrcstdoutstderrTz--first-time )r   r   appendextendr   shlexsplitr   r   run_command	fail_jsonresultmodule_loadedr   warn)r   commandr$   outerrr%   r&   s          r   load_modulezModprobe.load_modulet   s   $$%??NN4 		{U[[%==>{{..w7C7(4;;((_SRC_SWS^S^__??d002DL!%!8!8""D.$))Du{{SWS^S^G__"B Qw  ( r   c                     | j                   D ]D  }t        |      5 }|D ](  }| j                  j                  |      s d d d         y 	 d d d        F y# 1 sw Y   QxY w)NTF)modules_filesopenr   match)r   module_filefilelines       r   module_is_loaded_persistentlyz&Modprobe.module_is_loaded_persistently   sp    -- 	$Kk" $d  $D**006#$ $$$ $	$ $ $s   !AAAA	c                 f    t        | j                  j                               }|| j                  k(  S N)setr   r+   permanent_params)r   desired_paramss     r   params_is_setzModprobe.params_is_set   s*    T[[..01!6!666r   c                    t               }| j                  D ]\  }t        |      5 }|D ]@  }| j                  j	                  |      }|s!|j                  |j                  d             B 	 d d d        ^ |S # 1 sw Y   jxY w)N   )r?   modprobe_filesr7   r   r8   addgroup)r   r   modprobe_filer:   r;   r8   s         r   r@   zModprobe.permanent_params   s    !00 	3Mm$ 3  3D 99??EE

5;;q>233 3	3 3 3s   #A8	"A88B	c                     t         j                  j                  t        | j                  dz         }| j
                  s4t        |d      5 }|j                  | j                  dz          d d d        y y # 1 sw Y   y xY w)N.confw
)ospathjoinMODULES_LOAD_LOCATIONr   r   r7   write)r   	file_pathr:   s      r   create_module_filezModprobe.create_module_file   sj    GGLL!6!%W!46	i% -

499t+,- - - -s   
A33A<c                     | j                   j                         D cg c]  }dj                  | j                  |        }}dj	                  |      dz   S c c}w )Nzoptions {0} {1}rL   )r   r+   r   r   rO   )r   paramfile_contents      r   module_options_file_contentz$Modprobe.module_options_file_content   sX     &*[[%6%6%8:! *00EB : :yy&--:s   #Ac                     t         j                  j                  t        | j                  dz         }| j
                  s1t        |d      5 }|j                  | j                         d d d        y y # 1 sw Y   y xY w)NrJ   rK   )	rM   rN   rO   PARAMETERS_FILES_LOCATIONr   r   r7   rQ   rW   )r   new_file_pathr:   s      r   create_module_options_filez#Modprobe.create_module_options_file   sg    %>%)YY%8:mS) =T

4;;<= = = =s   
A00A9c                    | j                   D ]  }t        |      5 }|j                         }d d d        d}t              D ]+  \  }}| j                  j                  |      s"d|z   ||<   d}- | j                  ro|srt        |d      5 }|j                  dj                  |             d d d         y # 1 sw Y   xY w# 1 sw Y   xY wNF#TrK   rL   )	rE   r7   	readlines	enumerater   r8   r   rQ   rO   )r   rH   r:   rV   content_changedindexr;   s          r   disable_old_paramszModprobe.disable_old_params   s    !00 	8Mm$ 0#~~/0 $O(6 +t&&,,T2*-*L'&*O+
 ??-- 8JJtyy678 8	80 08 8   B7!C7C 	C	c                    | j                   D ]  }t        |      5 }|j                         }d d d        d}t              D ]+  \  }}| j                  j                  |      s"d|z   ||<   d}- | j                  ro|srt        |d      5 }|j                  dj                  |             d d d         y # 1 sw Y   xY w# 1 sw Y   xY wr]   )	r6   r7   r_   r`   r   r8   r   rQ   rO   )r   r9   r:   rV   ra   rb   r;   s          r   disable_module_permanentz!Modprobe.disable_module_permanent   s    -- 	8Kk" 0d#~~/0 $O(6 +t&&,,T2*-*L'&*O+
 ??+s+ 8tJJtyy678 8	80 08 8rd   c                     | j                   s| j                          d| _        | j                  s(| j	                          | j                          d| _        y y NT)r<   rS   r   rB   rc   r[   r   s    r   load_module_permanentzModprobe.load_module_permanent   sK    11##%DL!!##%++-DL "r   c                     | j                   r| j                          d| _        | j                  r| j	                          d| _        y y rh   )r<   rf   r   r@   rc   ri   s    r   unload_module_permanentz Modprobe.unload_module_permanent   s?    --))+DL  ##%DL !r   c                 P   t         j                  j                  t              sg S t        j                  t              D cg c]&  }t         j                  j                  t        |      ( }}|D cg c]$  }t         j                  j                  |      s#|& c}S c c}w c c}w r>   )rM   rN   isdirrP   listdirrO   isfiler   rN   modules_pathss      r   r6   zModprobe.modules_files   s{    ww}}23I%'ZZ0E%FH! &;TB H H!.G"''..2FGGHG   +B2$B#B#c                 P   t         j                  j                  t              sg S t        j                  t              D cg c]&  }t         j                  j                  t        |      ( }}|D cg c]$  }t         j                  j                  |      s#|& c}S c c}w c c}w r>   )rM   rN   rn   rY   ro   rO   rp   rq   s      r   rE   zModprobe.modprobe_files   s{    ww}}67I%'ZZ0I%JL! &?F L L!.G"''..2FGGLGrs   c                    d}	 t        d      5 }| j                  j                  dd      dz   }|D ]  }|j                  |      sd} n d d d        |swd| j                  z   dz   }t        j
                  j                  d	t        d
      }t        |      5 }|D ]%  }|j                         j                  |      s#d} n d d d        |S 	 |S # 1 sw Y   xY w# 1 sw Y   |S xY w# t        t        f$ rO} | j                  j                  dt        |      t        j                          d| j"                   Y d }~|S d }~ww xY w)NFz/proc/modules-_ T/z.koz/lib/modules/zmodules.builtin)r#   	exceptionr'   )r7   r   replace
startswithrM   rN   rO   RELEASE_VERrstripendswithIOErrorOSErrorr   r-   r   	traceback
format_excr.   )	r   	is_loadedmodulesmodule_namer;   r9   builtin_pathbuiltinses	            r   r/   zModprobe.module_loaded   sJ   		eo& '"ii//S9C?# D{3$(	 !DIIo5!ww||O[J[\,' "8 ( ";;=11+>(,I!""   % "  ! 	e!DKK!!dili>R>R>TdX\XcXcd	esZ   C( 6CCAC( %C=CC( CC( C% C( %C( (E7AEEc                    | j                   d| j                  g}| j                  r|j                  d       | j                  j                  |      \  }}}|dk7  r+ | j                  j                  d||||d| j                  S d| _        y )Nz-rr!   r   r"   Tr'   )	r   r   r   r(   r   r,   r-   r.   r   )r   r1   r$   r2   r3   s        r   unload_modulezModprobe.unload_module  s    $$dDII6??NN4 {{..w7C7(4;;((_SRC_SWS^S^__r   c                 `    | j                   | j                  | j                  | j                  dS )N)r   r   r   r   )r   r   r   r   ri   s    r   r.   zModprobe.result  s,     ||IIkk''	
 	
r   N)__name__
__module____qualname__r   r4   propertyr<   rB   r@   rS   rW   r[   rc   rf   rj   rl   r6   rE   r/   r   r.   r'   r   r   r	   r	   b   s    r )(   7 7
 
 
- . .
=8 8 	   H H H H.	 
 
r   r	   c                      t        t        t        dd      t        ddddg      t        dd      t        dd	g d
            d      S )NstrT)typerequiredpresentabsent)r   defaultchoices )r   r   disabled)r   r   r   )r   r   r   r   )argument_specsupports_check_mode)r   dictr'   r   r   build_moduler   )  sN    540E9x>STUB/
Def	
 ! r   c                     t               } t        |       }|j                  dk(  r!|j                         s|j	                          n/|j                  dk(  r |j                         r|j                          |j                  dk(  r)|j                  r|j                  s|j                          n7|j                  dk(  r(|j                  s|j                  r|j                           | j                  di |j                   y )Nr   r   r'   )r   r	   r   r/   r4   r   r   r<   rB   rj   r@   rl   	exit_jsonr.   )r   r   s     r   mainr   5  s    ^FH*83I3I3K			8	+0F0F0H i'1W1W\d\r\r&&(				(h.T.TX`XqXq((*F'x'r   __main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESos.pathrM   platformr*   r   r   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   releaser}   rP   rY   objectr	   r   r   r   r'   r   r   <module>r      s    A @3j(     	 4 Ah - - D
v D
N	($ zF r   