
    VhP6                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlZd dlZd dlZdadZ ej                   d	      Zdd
Zd ZddZd ZddZd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: dnf_versionlock
version_added: '4.0.0'
short_description: Locks package versions in C(dnf) based systems
description:
  - Locks package versions using the C(versionlock) plugin in C(dnf) based systems. This plugin takes a set of name and versions
    for packages and excludes all other versions of those packages. This allows you to for example protect packages from being
    updated by newer versions. The state of the plugin that reflects locking of packages is the C(locklist).
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: partial
    details:
      - The logics of the C(versionlock) plugin for corner cases could be confusing, so please take in account that this module
        will do its best to give a C(check_mode) prediction on what is going to happen. In case of doubt, check the documentation
        of the plugin.
      - Sometimes the module could predict changes in C(check_mode) that will not be such because C(versionlock) concludes
        that there is already a entry in C(locklist) that already matches.
  diff_mode:
    support: none
options:
  name:
    description:
      - Package name spec to add or exclude to or delete from the C(locklist) using the format expected by the C(dnf repoquery)
        command.
      - This parameter is mutually exclusive with O(state=clean).
    type: list
    required: false
    elements: str
    default: []
  raw:
    description:
      - Do not resolve package name specs to NEVRAs to find specific version to lock to. Instead the package name specs are
        used as they are. This enables locking to not yet available versions of the package.
    type: bool
    default: false
  state:
    description:
      - Whether to add (V(present) or V(excluded)) to or remove (V(absent) or V(clean)) from the C(locklist).
      - V(present) will add a package name spec to the C(locklist). If there is a installed package that matches, then only
        that version will be added. Otherwise, all available package versions will be added.
      - V(excluded) will add a package name spec as excluded to the C(locklist). It means that packages represented by the
        package name spec will be excluded from transaction operations. All available package versions will be added.
      - V(absent) will delete entries in the C(locklist) that match the package name spec.
      - V(clean) will delete all entries in the C(locklist). This option is mutually exclusive with O(name).
    choices: ['absent', 'clean', 'excluded', 'present']
    type: str
    default: present
notes:
  - In an ideal world, the C(versionlock) plugin would have a dry-run option to know for sure what is going to happen. So
    far we have to work with a best guess as close as possible to the behaviour inferred from its code.
  - For most of cases where you want to lock and unlock specific versions of a package, this works fairly well.
  - Does not work with C(dnf5).
requirements:
  - dnf
  - dnf-plugin-versionlock
author:
  - Roberto Moreda (@moreda) <moreda@allenta.com>
a  
- name: Prevent installed nginx from being updated
  community.general.dnf_versionlock:
    name: nginx
    state: present

- name: Prevent multiple packages from being updated
  community.general.dnf_versionlock:
    name:
      - nginx
      - haproxy
    state: present

- name: Remove lock from nginx to be updated again
  community.general.dnf_versionlock:
    package: nginx
    state: absent

- name: Exclude bind 32:9.11 from installs or updates
  community.general.dnf_versionlock:
    package: bind-32:9.11*
    state: excluded

- name: Keep bash package in major version 4
  community.general.dnf_versionlock:
    name: bash-0:4.*
    raw: true
    state: present

- name: Delete all entries in the locklist of versionlock
  community.general.dnf_versionlock:
    state: clean
a  
locklist_pre:
  description: Locklist before module execution.
  returned: success
  type: list
  elements: str
  sample: ['bash-0:4.4.20-1.el8_4.*', '!bind-32:9.11.26-4.el8_4.*']
locklist_post:
  description: Locklist after module execution.
  returned: success and (not check mode or state is clean)
  type: list
  elements: str
  sample: ['bash-0:4.4.20-1.el8_4.*']
specs_toadd:
  description: Package name specs meant to be added by versionlock.
  returned: success
  type: list
  elements: str
  sample: ['bash']
specs_todelete:
  description: Package name specs meant to be deleted by versionlock.
  returned: success
  type: list
  elements: str
  sample: ['bind']
)AnsibleModuleNz/usr/bin/dnfz!/etc/dnf/plugins/versionlock.confzK^(?P<name>.+)-(?P<epoch>\d+):(?P<version>.+)-(?P<release>.+)\.(?P<arch>.+)$c                    |sg n|}|rdgng }|rSg }|D ]9  }| j                  t        dd|g|z   |gz   d      \  }}}	|j                  |       ; dj                  |      }|S | j                  t        dd|gd      \  }}}	|S )Nz--raw-qversionlockTcheck_rc
)run_commandDNF_BINappendjoin)
modulecommandpatternsrawraw_parameteroutsprcouterrs
             u/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/dnf_versionlock.pydo_versionlockr      s    !rxH!$WI"M  	A!--$w7-G1#M . LBS KK		
 iio J ))dM73d * DCJ    c                    | j                  d      } | |k(  ryt        j                  |       }|syd|d   z  |d   d|d   |d   d|d	   |d   d|d	   d|d
   |d   d|d   d|d	   |d   d|d	   d|d
   d|d   |d   d|d   d|d	   d|d
   |d   d|d   d|d	   d|d
   d|d   	|d   d|d   d|d	   d|d
   d|d   	f	D ]  }t        j                  ||      s y y)N!TFz%sname.arch-versionreleaseepoch:)lstripNEVRA_REmatchfnmatch)entrypatternmr    s       r   r*   r*      s2   LLEuAqyV9ai(V9a	l+i9q|<i7Qy\:6AiL!I,&	J6AgJ)a	lKgJ&	1Y<9fI	'fIqz1Y<9fI	'  ??4) r   c                 r   i }| j                  t        ddg|rdgng z   |z   d      \  }}}|j                         D ]x  }t        j	                  |      }|s| j                  d|z  |||       |d   d	|d
   d|d   }	|j                  |d   t                      ||d      j                  |	       z |S )Nr   	repoqueryz--installedTr
   zfailed to parse nevra for %s)msgr   r   r   r&   r'   r$   r#   r%   r    )	r   r   splitr)   r*   	fail_json
setdefaultsetadd)
r   r   only_installed packages_available_map_name_evrsr   r   r   r   r.   evrs
             r   get_packagesr:      s    ')$%%	$$*-	4	 	 & LBS YY[ =NN12Q63C  ) gJiLiL* 	)33AfIsuE(6377<= ,+r   c                      t         fD ]K  } t        j                  j                  |       s#t        j                  j	                  |       dk(  rdc S dc S  y)Nz/usr/bin/dnf5dnf5dnf)r   ospathexistsrealpath)bin_paths    r   get_package_mgrrC      sL    J V77>>(#WW--h7?J6UPUUV r   c                 N   |dk(  rt        | d      j                         S g }|dk(  rd}d }t        | d      j                         D ]  }|j                  d      r|j                  d      red}|j                  dd	      \  }}|j	                         }t        | |g
      }|d||   j                         d}|r||vr|j                  |       |j                  d      s|j                  dd	      \  }}	|	j	                         }	|s|r||vr|j                  |       d} |S )Nr=   listr<   F)# zPackage name:Tr'      )r   r#   .*r9   =)r   r2   
splitlines
startswithstripr:   popr   )
r   package_mgrpackage_liststanza_startpackage_namelinedummyr    pkg_namepackage_versions
             r   get_package_listrW      s)   eff-3355Lf"662==? 	)Dz*/#"jja0tzz|'$@,0(4.2D2D2FGL$D ''5u%)-C);&"1"7"7"9#L(H$++L9#(L#	)$ r   c                  :   t        t        t        ddg       t        dd      t        ddg d	      
      d      } | j                  d   }| j                  d   }| j                  d   }d}d}| j                  dd      at               }|dk(  r5t        j                  j                  t              s| j                  d       |dk(  r|r| j                  d       |dk7  r|s| j                  d|z         t        | |      }g }g }	|dv r|r(|D ]"  }
|dk(  r|
nd|
z   |vs|j                  |
       $ ndt        | |      }t        | |d      }|j                  |       |D ]4  }||   D ]*  }|d|d}|dk(  r|nd|z  |vs|j                  |       , 6 |r| j                  s|dk(  rdnd}t!        | |||       }n|d!k(  rj|r|D ]  }
|
|v s|	j                  |
        n,|D ]'  }
|D ]   }t#        ||
      s|	j                  |
       " ) |	r=| j                  s1t!        | d"|	|       }n!|dk(  r|}	|	r| j                  st!        | d#      }|s|	rd}|||||	d$}| j                  st        | |      |d%<   n
|dk(  rg |d%<    | j$                  d&i | y )'NrE   str)typeelementsdefaultboolF)rZ   r\   present)r^   absentexcludedclean)rZ   r\   choices)r    r   stateT)argument_specsupports_check_moder    r   rc    r=   zplugin versionlock is required)r1   ra   z,clean state is incompatible with a name listz"name list is required for %s state)rO   )r^   r`   r   )r7   r#   rI   z!%sr6   exclude)r   r   r_   deleteclear)changedr1   locklist_prespecs_toaddspecs_todeletelocklist_post )r   dictparamsget_bin_pathr   rC   r>   r?   r@   VERSIONLOCK_CONFr3   rW   r   r:   update
check_moder   r*   	exit_json)r   r   r   rc   rj   r1   rO   rk   rl   rm   r   packages_map_name_evrs packages_installed_map_name_evrsr    r9   locklist_entrycmderesponses                      r   mainr}     s   6E2>&%0E9IK
 !F }}V$H
--
CMM'"EG
C !!%.G!#KeBGGNN3C$D=> HKLAEIJ#FDLKN'' *9,Q#'+,&&q)* &2&"
 0<#0%, #))*JK. ;1$7 ;C26%<N*/9*<!&!7M#**>:;; v00 I-%9C {LC	(	 -$"))!,-  1% 1AQ{&--a011
 &"3"3 >sDC 
'	%&"3"3 1Cn $"(H $4V$U!G(*H_%F x r   __main__)NF)F)r=   )
__future__r   r   r   rZ   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   r+   r>   rer   rs   compiler)   r   r*   r:   rC   rW   r}   __name__ro   r   r   <module>r      s    C B;z D
6 5  	 	
6 2:: 8 9>2,2:r!j zF r   