a ì)gÕFã@sddlmZmZmZeZdZdZdZddl Z ddl m Z m Z ddl mZmZddlmZdd lmZdd lmZmZmZdd lmZmZmZGd d „d eƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZ Gdd„deƒZ!Gdd„deƒZ"dd„Z#e$dk�re#ƒdS)é)Úabsolute_importÚdivisionÚprint_functiona8 module: package_facts short_description: Package information as facts description: - Return information about installed packages as facts. options: manager: description: - The package manager used by the system so we can query the package information. - Since 2.8 this is a list and can support multiple package managers per system. - The 'portage' and 'pkg' options were added in version 2.8. - The 'apk' option was added in version 2.11. - The 'pkg_info' option was added in version 2.13. default: ['auto'] choices: ['auto', 'rpm', 'apt', 'portage', 'pkg', 'pacman', 'apk', 'pkg_info'] type: list elements: str strategy: description: - This option controls how the module queries the package managers on the system. C(first) means it will return only information for the first supported package manager available. C(all) will return information for all supported and available package managers on the system. choices: ['first', 'all'] default: 'first' type: str version_added: "2.8" version_added: "2.5" requirements: - For 'portage' support it requires the C(qlist) utility, which is part of 'app-portage/portage-utils'. - For Debian-based systems C(python-apt) package must be installed on targeted hosts. - For SUSE-based systems C(python3-rpm) package must be installed on targeted hosts. This package is required because SUSE does not include RPM Python bindings by default. author: - Matthew Jones (@matburt) - Brian Coca (@bcoca) - Adam Miller (@maxamillion) extends_documentation_fragment: - action_common_attributes - action_common_attributes.facts attributes: check_mode: support: full diff_mode: support: none facts: support: full platform: platforms: posix a� - name: Gather the package facts ansible.builtin.package_facts: manager: auto - name: Print the package facts ansible.builtin.debug: var: ansible_facts.packages - name: Check whether a package called foobar is installed ansible.builtin.debug: msg: "{{ ansible_facts.packages['foobar'] | length }} versions of foobar are installed!" when: "'foobar' in ansible_facts.packages" aª ansible_facts: description: Facts to add to ansible_facts. returned: always type: complex contains: packages: description: - Maps the package name to a non-empty list of dicts with package information. - Every dict in the list corresponds to one installed version of the package. - The fields described below are present for all package managers. Depending on the package manager, there might be more fields for a package. returned: when operating system level package manager is specified or auto detected manager type: dict contains: name: description: The package's name. returned: always type: str version: description: The package's version. returned: always type: str source: description: Where information on the package came from. returned: always type: str sample: |- { "packages": { "kernel": [ { "name": "kernel", "source": "rpm", "version": "3.10.0", ... }, { "name": "kernel", "source": "rpm", "version": "3.10.0", ... }, ... ], "kernel-tools": [ { "name": "kernel-tools", "source": "rpm", "version": "3.10.0", ... } ], ... } } # Sample rpm { "packages": { "kernel": [ { "arch": "x86_64", "epoch": null, "name": "kernel", "release": "514.26.2.el7", "source": "rpm", "version": "3.10.0" }, { "arch": "x86_64", "epoch": null, "name": "kernel", "release": "514.16.1.el7", "source": "rpm", "version": "3.10.0" }, { "arch": "x86_64", "epoch": null, "name": "kernel", "release": "514.10.2.el7", "source": "rpm", "version": "3.10.0" }, { "arch": "x86_64", "epoch": null, "name": "kernel", "release": "514.21.1.el7", "source": "rpm", "version": "3.10.0" }, { "arch": "x86_64", "epoch": null, "name": "kernel", "release": "693.2.2.el7", "source": "rpm", "version": "3.10.0" } ], "kernel-tools": [ { "arch": "x86_64", "epoch": null, "name": "kernel-tools", "release": "693.2.2.el7", "source": "rpm", "version": "3.10.0" } ], "kernel-tools-libs": [ { "arch": "x86_64", "epoch": null, "name": "kernel-tools-libs", "release": "693.2.2.el7", "source": "rpm", "version": "3.10.0" } ], } } # Sample deb { "packages": { "libbz2-1.0": [ { "version": "1.0.6-5", "source": "apt", "arch": "amd64", "name": "libbz2-1.0" } ], "patch": [ { "version": "2.7.1-4ubuntu1", "source": "apt", "arch": "amd64", "name": "patch" } ], } } # Sample pkg_info { "packages": { "curl": [ { "name": "curl", "source": "pkg_info", "version": "7.79.0" } ], "intel-firmware": [ { "name": "intel-firmware", "source": "pkg_info", "version": "20210608v0" } ], } } N)Ú to_nativeÚto_text)Ú AnsibleModuleÚmissing_required_lib)Úget_best_parsable_locale)Ú get_bin_path)Ú has_respawnedÚprobe_interpreters_for_moduleÚrespawn_module)ÚLibMgrÚCLIMgrÚget_all_pkg_managerscs0eZdZdZdd„Zdd„Z‡fdd„Z‡ZS)ÚRPMÚrpmcCs|j ¡ ¡S©N)Ú_libZTransactionSetZdbMatch©Úself©rúA/usr/lib/python3.9/site-packages/ansible/modules/package_facts.pyÚlist_installedÿszRPM.list_installedcCs:t||jj||jj||jj||jj||jjd�S)N)ÚnameÚversionÚreleaseZepochÚarch)ÚdictrZ RPMTAG_NAMEZRPMTAG_VERSIONZRPMTAG_RELEASEZ RPMTAG_EPOCHZ RPMTAG_ARCH©rÚpackagerrrÚget_package_detailss      üzRPM.get_package_detailscsttt|ƒ ¡}zNtdƒ|sBtƒsBgd¢}t||jƒ}|rBt|ƒ|sZt  dt |jƒ¡Wnt ynYn0|S)zh we expect the python bindings installed, but this gives warning if they are missing and we have rpm clir)z/usr/libexec/platform-pythonú/usr/bin/python3ú/usr/bin/python2zFound "rpm" but %s) ÚsuperrÚ is_availabler r r ÚLIBr ÚmoduleÚwarnrÚ ValueError)rÚ we_have_libÚ interpretersÚinterpreter_path©Ú __class__rrr% s   zRPM.is_available)Ú__name__Ú __module__Ú __qualname__r&rr!r%Ú __classcell__rrr-rrûsrcsHeZdZdZ‡fdd„Zedd„ƒZ‡fdd„Zdd „Zd d „Z ‡Z S) ÚAPTÚaptcsd|_tt|ƒ ¡dSr)Ú_cacher$r3Ú__init__rr-rrr6&sz APT.__init__cCs"|jdur|jS|j ¡|_|jSr)r5rZCacherrrrÚ pkg_cache*s  z APT.pkg_cachec s„tt|ƒ ¡}|s€dD]h}z t|ƒWnty<YqYq0tƒsdddg}t||jƒ}|rdt|ƒt   d|t dƒf¡q€q|S)zi we expect the python bindings installed, but if there is apt/apt-get give warning about missing bindings)r4zapt-getZaptituder"r#zFound "%s" but %sr4) r$r3r%r r)r r r&r r'r(r)rr*Zexer+r,r-rrr%2s"   ÿ zAPT.is_availablecs|j‰‡fdd„ˆ ¡DƒS)Ncsg|]}ˆ|jr|‘qSr)Z is_installed)Ú.0Zpk©ÚcacherrÚ Móz&APT.list_installed..)r7Úkeysrrr9rrJszAPT.list_installedcCs,|j|j}t||j|j|j|jdjd�S)Nr)rrrÚcategoryÚorigin)r7Ú installedrrZ architectureÚsectionZoriginsr?)rr Zac_pkgrrrr!Os zAPT.get_package_details) r/r0r1r&r6Úpropertyr7r%rr!r2rrr-rr3"s   r3c@s eZdZdZdd„Zdd„ZdS)ÚPACMANZpacmancCsVttƒ}tj|jdgt|d�d�\}}}|dks4|rDtd||fƒ‚| d¡dd…S)Nz-Qi)ÚLC_ALL)Zenviron_updaterú"Unable to list packages rc=%s : %sz éÿÿÿÿ)r r'Ú run_commandÚ_clirÚ ExceptionÚsplit)rÚlocaleÚrcÚoutÚerrrrrrXs   zPACMAN.list_installedcCsši}d}| ¡D]F}t d|¡}|r>| d¡}| d¡||<q||d| ¡||<qd}|ddkr€dd„|d d¡Dƒ}|d |d |d |d œS) Nz([\w ]*[\w]) +: (.*)ééz ZProvidesÚNonecSsg|]}| d¡d‘qS)ú=r)rJ)r8Úprrrr;nsÿz.PACMAN.get_package_details..ÚNameZVersionZ Architecture)rrrÚprovides)Ú splitlinesÚreÚmatchÚgroupÚlstriprJ)rr Úraw_pkg_detailsZ last_detailÚlineÚmrUrrrr!_s$     þüzPACMAN.get_package_detailsN©r/r0r1ÚCLIrr!rrrrrCTsrCc@s(eZdZdZgd¢Zdd„Zdd„ZdS)ÚPKGÚpkg) rrr?r@Ú automaticrr>ÚprefixÚvitalc CsJt |jddd gd¢¡g¡\}}}|dks2|rBtd||fƒ‚| ¡S)NÚqueryz%%%sz %) ÚnÚvÚRÚtÚaÚqÚorSÚVrrE)r'rGrHÚjoinrIrV©rrLrMrNrrrr€s& zPKG.list_installedcCstt|j| d¡ƒƒ}d|vrLz|d d¡d|d<WntyJYn0d|vrhtt|dƒƒ|d<d|vrˆ|d dd¡d |d<d |vrôd |d vrº|d  d d¡\|d <|d <nd |d <d |d vrì|d  d d¡\|d <|d<nd|d<d|v�rtt|dƒƒ|d<|S)Nú rú:rPrbr>ú/rOrrú,Z port_epochÚ_ZrevisionÚ0rd)rÚzipÚatomsrJÚ IndexErrorÚboolÚint)rr rarrrr!†s(    zPKG.get_package_detailsN©r/r0r1r_rwrr!rrrrr`{sr`c@s(eZdZdZgd¢Zdd„Zdd„ZdS)ÚPORTAGEZqlist)r>rrZebuild_revisionÚslotsÚprefixesZsufixesc CsNtjd |jddddddg¡dd �\}}}|d krFtd |t|ƒfƒ‚| ¡S) Nú z-Ivú|Zxargsz-nZ1024ZqatomT)Zuse_unsafe_shellrrE)r'rGrnrHÚ RuntimeErrorrrVrorrrr¬s*zPORTAGE.list_installedcCstt|j| ¡ƒƒSr)rrvrwrJrrrrr!²szPORTAGE.get_package_detailsNr{rrrrr|§sr|c@s eZdZdZdd„Zdd„ZdS)ÚAPKZapkcCs<t |jddg¡\}}}|dks$|r4td||fƒ‚| ¡S)NÚinfoz-vrrE©r'rGrHrIrVrorrrrºs zAPK.list_installedcCsN|dddœ}| dd¡}z|d|d|ddœWStyH|YS0dS)NÚ)rrrú-rPrrO)Úrsplitrx)rr r[Znvrrrrr!Às  ý zAPK.get_package_detailsNr^rrrrr‚¶sr‚c@s eZdZdZdd„Zdd„ZdS)ÚPKG_INFOZpkg_infocCs:t |jdg¡\}}}|dks"|r2td||fƒ‚| ¡S)Nz-arrEr„rorrrrÑs zPKG_INFO.list_installedcCsT|ddœ}|jdd�djddd�}z|d|ddœWStyN|YS0dS)Nr…)rrrO)Úmaxsplitrr†)rJr‡rx)rr r[Údetailsrrrr!×s þ zPKG_INFO.get_package_detailsNr^rrrrrˆÍsrˆc Cstƒ}dd„| ¡Dƒ}ttdddgdœddgdd œd �d d �ai}d ii}dd„tjdDƒ}tjd}d|vr†| |¡| d¡t|ƒ  |¡}|rÆdtjdvr¬d}ndd  |¡}tj |d�d}tƒ} |D�]} |rì|dkrì�qÞ| | vröqÔ|   | ¡zŽz.|| ƒ} |   ¡�r.|d7}| |  ¡¡WnZt�yŠ} z@| tjdv�rht d| t| ƒf¡WYd} ~ WqÔWYd} ~ n d} ~ 00WqÔt�yÚ} z2| tjdv�rÆt d| t| ƒf¡WYd} ~ qÔd} ~ 00qÔ|dk�rüd|}tj |d�||d d<tjfi|¤ŽdS)NcSsg|] }| ¡‘qSr©Úlower©r8Úxrrrr;èr<zmain..ÚlistÚstrÚauto)ÚtypeÚelementsÚdefaultÚfirstÚall)Úchoicesr”)ÚmanagerÚstrategyT)Z argument_specZsupports_check_modeZ ansible_factscSsg|] }| ¡‘qSrr‹r�rrrr;ñr<r˜r™zKCould not auto detect a usable package manager, check warnings for details.z*Unsupported package managers requested: %sz, )ÚmsgrrOz>Requested package manager %s was not usable by this module: %sz'Failed to retrieve packages with %s: %sz–Could not detect a supported package manager from the following list: %s, or the required Python library is not installed. Check warnings for details.Úpackages)rr=rrr'ÚparamsÚextendÚremoveÚsetÚ differencernZ fail_jsonÚaddr%ÚupdateZ get_packagesrIr(rZ exit_json) Z PKG_MANAGERSZPKG_MANAGER_NAMESr›ÚresultsZmanagersr™Z unsupportedršÚfoundÚseenZpkgmgrr˜ÚerrrÚmainäs` ÿþ         (. ÿ  r§Ú__main__)%Z __future__rrrr’Z __metaclass__Z DOCUMENTATIONZEXAMPLESZRETURNrWZansible.module_utils._textrrZansible.module_utils.basicrrZ"ansible.module_utils.common.localer Z#ansible.module_utils.common.processr Z#ansible.module_utils.common.respawnr r r Z#ansible.module_utils.facts.packagesrrrrr3rCr`r|r‚rˆr§r/rrrrÚs,2&  '2',E