a ì)gŒqã@srddlmZmZmZeZdZdZdZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlmZddlmZmZmZddlmZdd lmZdd lmZz,ddlZddlZddl m!Z"e" #¡Z!d Z$Wn(e%ydZZZ"Z!d Z$Yn0gd ¢Z&dZ'dZ(dd„Z)Gdd„de*ƒZ+Gdd„de,ƒZ-Gdd„de-ƒZ.dd„Z/dd„Z0e1dkrne0ƒdS)é)Úabsolute_importÚdivisionÚprint_functionaú --- module: apt_repository short_description: Add and remove APT repositories description: - Add or remove an APT repositories in Ubuntu and Debian. extends_documentation_fragment: action_common_attributes attributes: check_mode: support: full diff_mode: support: full platform: platforms: debian notes: - This module supports Debian Squeeze (version 6) as well as its successors and derivatives. options: repo: description: - A source string for the repository. type: str required: true state: description: - A source string state. type: str choices: [ absent, present ] default: "present" mode: description: - The octal mode for newly created files in sources.list.d. - Default is what system uses (probably 0644). type: raw version_added: "1.6" update_cache: description: - Run the equivalent of C(apt-get update) when a change occurs. Cache updates are run after making changes. type: bool default: "yes" aliases: [ update-cache ] update_cache_retries: description: - Amount of retries if the cache update fails. Also see I(update_cache_retry_max_delay). type: int default: 5 version_added: '2.10' update_cache_retry_max_delay: description: - Use an exponential backoff delay for each retry (see I(update_cache_retries)) up to this max delay in seconds. type: int default: 12 version_added: '2.10' validate_certs: description: - If C(false), SSL certificates for the target repo will not be validated. This should only be used on personally controlled sites using self-signed certificates. type: bool default: 'yes' version_added: '1.8' filename: description: - Sets the name of the source list file in sources.list.d. Defaults to a file name based on the repository source url. The .list extension will be automatically added. type: str version_added: '2.1' codename: description: - Override the distribution codename to use for PPA repositories. Should usually only be set when working with a PPA on a non-Ubuntu target (for example, Debian or Mint). type: str version_added: '2.3' install_python_apt: description: - Whether to automatically try to install the Python apt library or not, if it is not already installed. Without this library, the module does not work. - Runs C(apt-get install python-apt) for Python 2, and C(apt-get install python3-apt) for Python 3. - Only works with the system Python 2 or Python 3. If you are using a Python on the remote that is not the system Python, set I(install_python_apt=false) and ensure that the Python apt library for your Python version is installed some other way. type: bool default: true author: - Alexander Saltanov (@sashka) version_added: "0.7" requirements: - python-apt (python 2) - python3-apt (python 3) - apt-key or gpg a! - name: Add specified repository into sources list ansible.builtin.apt_repository: repo: deb http://archive.canonical.com/ubuntu hardy partner state: present - name: Add specified repository into sources list using specified filename ansible.builtin.apt_repository: repo: deb http://dl.google.com/linux/chrome/deb/ stable main state: present filename: google-chrome - name: Add source repository into sources list ansible.builtin.apt_repository: repo: deb-src http://archive.canonical.com/ubuntu hardy partner state: present - name: Remove specified repository from sources list ansible.builtin.apt_repository: repo: deb http://archive.canonical.com/ubuntu hardy partner state: absent - name: Add nginx stable repository from PPA and install its signing key on Ubuntu target ansible.builtin.apt_repository: repo: ppa:nginx/stable - name: Add nginx stable repository from PPA and install its signing key on Debian target ansible.builtin.apt_repository: repo: 'ppa:nginx/stable' codename: trusty - name: One way to avoid apt_key once it is removed from your distro block: - name: somerepo |no apt key ansible.builtin.get_url: url: https://download.example.com/linux/ubuntu/gpg dest: /etc/apt/trusted.gpg.d/somerepo.asc - name: somerepo | apt source ansible.builtin.apt_repository: repo: "deb [arch=amd64 signed-by=/etc/apt/trusted.gpg.d/myrepo.asc] https://download.example.com/linux/ubuntu {{ ansible_distribution_release }} stable" state: present ú#N)Ú AnsibleModule)Ú has_respawnedÚprobe_interpreters_for_moduleÚrespawn_module)Ú to_native)ÚPY3)Ú fetch_urlTF)z/etc/apt/keyringsz/etc/apt/trusted.gpg.dz/usr/share/keyringsi¤)Zdebzdeb-srccCs˜|js„| d¡}|r”| |dg¡\}}}|dkrH|jd|| ¡fd| |d|ddg¡\}}}|dkr”|jd|| ¡fdn|jd |ddS) Nzapt-getÚupdaterz*Failed to auto-install %s. Error was: '%s'©ÚmsgÚinstallz-yz-qz&%s must be installed to use check mode)Ú check_modeÚ get_bin_pathÚ run_commandÚ fail_jsonÚstrip)ÚmoduleÚ apt_pkg_nameZ apt_get_pathÚrcZsoZse©rúB/usr/lib/python3.9/site-packages/ansible/modules/apt_repository.pyÚinstall_python_apt¹s rc@s eZdZdS)Ú InvalidSourceN)Ú__name__Ú __module__Ú __qualname__rrrrrÈsrc@sšeZdZdd„Zdd„Zdd„Zdd„Zd$d d „Zed d „ƒZ edd„ƒZ dd„Z dd„Z dd„Z dd„Zd%dd„Zdd„Zd&dd„Zd d!„Zd"d#„ZdS)'Ú SourcesListcCsb||_i|_tƒ|_| d¡|_tj |j¡r:|  |j¡t   d|  d¡¡D]}|  |¡qNdS)NzDir::Etc::sourcelistz %s/*.listúDir::Etc::sourceparts) rÚfilesÚsetÚ new_reposÚ _apt_cfg_fileÚ default_fileÚosÚpathÚisfileÚloadÚglobZiglobÚ _apt_cfg_dir)ÚselfrÚfilerrrÚ__init__Ïs  zSourcesList.__init__ccs@|j ¡D]0\}}|D]"\}}}}}|r|||||fVqq dS)zeSimple iterator to go over all sources. Empty, non-source, and other not valid lines will be skipped.N)r"Úitems)r-r.ÚsourcesÚnÚvalidÚenabledÚsourceÚcommentrrrÚ__iter__ÞszSourcesList.__iter__cCs,d|vr |Stj tj | d¡|¡¡SdS)Nú/r!)r'r(ÚabspathÚjoinr,)r-ÚfilenamerrrÚ _expand_pathåszSourcesList._expand_pathcsl‡fdd„}dd„}t dd|¡}t dd|¡}dd „| ¡Dƒ}||d ƒ|d <d |d  |dd …¡ƒS)Ncs0ˆjjd}|dur|Sd t dd|¡ ¡¡S)Nr;Ú_z [^a-zA-Z0-9]ú )rÚparamsr:ÚreÚsubÚsplit)Úsr;©r-rrÚ_cleanup_filenameìs z8SourcesList._suggest_filename.._cleanup_filenamecSs d|vr| dd¡}|d}|S)Nú@ééÿÿÿÿ)rB)rCrrrÚ_strip_username_passwordòs z?SourcesList._suggest_filename.._strip_username_passwordz \[[^\]]+\]Úz\w+://cSsg|]}|tvr|‘qSr)ÚVALID_SOURCE_TYPES)Ú.0ÚpartrrrÚ ýóz1SourcesList._suggest_filename..rz%s.listr>rG)r@rArBr:)r-ÚlinerErIÚpartsrrDrÚ_suggest_filenameës zSourcesList._suggest_filenameFc Cs²d}d}d}d}| ¡}| d¡r2d}|dd…}| d¡}|dkrd||dd… ¡}|d|…}| ¡}|r’| ¡}|dtvr’d}d |¡}|r¦|rž|s¦t|ƒ‚||||fS)NFTrJrrGrr>)rÚ startswithÚfindrBrKr:r) r-rPÚraise_if_invalid_or_disabledr3r4r5r6ÚiÚchunksrrrÚ_parses*       zSourcesList._parsecCs4ztj |¡}Wnty.tj |¡}Yn0|S©zJ Wrapper for `apt_pkg` module for running with Python 2.5 )Úapt_pkgÚconfigZ find_fileÚAttributeErrorÚConfigZFindFile)ZfilespecÚresultrrrr%#s  zSourcesList._apt_cfg_filecCs4ztj |¡}Wnty.tj |¡}Yn0|SrY)rZr[Zfind_dirr\r]ZFindDir)Zdirspecr^rrrr,.s  zSourcesList._apt_cfg_dirc CsTg}t|dƒ}t|ƒD].\}}| |¡\}}}} | ||||| f¡q||j|<dS)NÚr)ÚopenÚ enumeraterXÚappendr") r-r.ÚgroupÚfr2rPr3r4r5r6rrrr*9s  zSourcesList.loadc Cst|j ¡ƒD]ê\}}|rÜtj |¡\}}zt |¡WnHty„}z0tj |¡sp|j   d|t |ƒf¡WYd}~n d}~00zt j d||d\}}WnBttfyâ}z&|j j d|t |ƒfdWYd}~n d}~00t |d¡} |D]ª\} } } } }g}| s| d¡| | ¡|r:| d¡| |¡| d ¡d  |¡}z|  |¡Wqôtyœ}z&|j j d |t |ƒfdWYd}~qôd}~00qô|j  ||¡||jvrú|j j d t¡}|j  ||d ¡q|j|=tj |¡rt |¡qdS)Nz!Failed to create directory %s: %sz.%s-)ÚprefixÚdirz5Unable to create temp file at "%s" for apt source: %srÚwú# ú # Ú rJzFailed to write to file %s: %sÚmodeF)Úlistr"r0r'r(rBÚmakedirsÚOSErrorÚisdirrrr ÚtempfileZmkstempÚIOErrorÚfdopenrbr:ÚwriteZ atomic_mover$r?ÚgetÚDEFAULT_SOURCES_PERMZset_mode_if_differentÚexistsÚremove)r-r;r1ÚdÚfnÚexÚfdZtmp_pathÚerdr2r3r4r5r6rWrPZ this_moderrrÚsaveAsD .0       2  zSourcesList.savec Cs”i}|j ¡D]€\}}|rg}|D]\\}}}}} g} |sB|  d¡|  |¡| rd|  d¡|  | ¡|  d¡| d | ¡¡q"d |¡||<q|S)NrhrirjrJ)r"r0rbr:) r-Z dumpstructr;r1Úlinesr2r3r4r5r6rWrrrÚdumpks      zSourcesList.dumpcCs|dur |S|S©Nr)r-ÚnewÚoldrrrÚ_choice}szSourcesList._choiceNc CsR|j||dd…\}}}} ||| ||¡| ||¡| || ¡f|j||<dS)zÀ This function to be used with iterator, so we don't care of invalid sources. If source, enabled, or comment is None, original value from line ``n`` will be preserved. rGN)r"rƒ) r-r.r2r4r5r6r3Z enabled_oldZ source_oldZ comment_oldrrrÚmodify‚szSourcesList.modifyc Cs®|j d|||f¡d}|D]*\}}}}} ||kr|j||ddd}q|sª|dur^|j}n | |¡}||jvr|g|j|<|j|} |  t| ƒdd||f¡|j  |¡dS)Nzading source file: %s | %s | %sFT)r4) rÚlogr„r&r<r"rbÚlenr$Úadd) r-Z source_newZ comment_newr.Úfoundr;r2r4r5r6r"rrrÚ_add_valid_sourceŠs    zSourcesList._add_valid_sourcerJcCs0|j|ddd}|j|||p&| |¡ddS)NT©rUé)r.)rXr‰rR)r-rPr6r.r5rrrÚ add_source¡szSourcesList.add_sourcecCs4|D]*\}}}}}||kr|r|j| |¡qdSr€)r"Úpop)r-r5r;r2r4Úsrcr6rrrÚ_remove_valid_source§s z SourcesList._remove_valid_sourcecCs |j|ddd}| |¡dS)NTrŠr‹)rXr©r-rPr5rrrÚ remove_source­szSourcesList.remove_source)F)NNN)rJN)rrrr/r7r<rRrXÚ staticmethodr%r,r*r}rrƒr„r‰rŒrr‘rrrrr Îs$   *  r csheZdZdZ‡fdd„Zddd„Zdd„Zd d „Zd d „Zd d„Z ddd„Z dd„Z e dd„ƒZ ‡ZS)ÚUbuntuSourcesListz-https://launchpad.net/api/1.0/~%s/+archive/%scsj||_|jdptj|_tt|ƒ |¡|jjddd|_|jjddd|_ |jsf|j sf|jj dddS)NÚcodenamezapt-keyF)ÚrequiredZgpgzDEither apt-key or gpg binary is required, but neither could be foundr) rr?Údistror”Úsuperr“r/rÚ apt_key_binÚgpg_binr)r-r©Ú __class__rrr/¶s zUbuntuSourcesList.__init__NcCs t|jƒSr€)r“r)r-ÚmemorrrÚ __deepcopy__ÀszUbuntuSourcesList.__deepcopy__cCs`|j||f}tdd}t|j||d\}}|ddkrN|jjd|ddt t| ¡ƒ¡S) Nzapplication/json)ZAccept)ÚheadersÚstatuséÈz.failed to fetch PPA information, error was: %srr) ÚLP_APIÚdictr rrÚjsonÚloadsr Úread)r-Z owner_nameÚppa_nameZlp_apiržZresponseÚinforrrÚ _get_ppa_infoÃs   zUbuntuSourcesList._get_ppa_infocCs`| d¡d}| d¡d}z| d¡d}WntyDd}Yn0d|||jf}|||fS)Nú:rGr8rÚppaz1deb http://ppa.launchpad.net/%s/%s/ubuntu %s main)rBÚ IndexErrorr”)r-r(rªÚ ppa_ownerr¦rPrrrÚ _expand_ppaÌs  zUbuntuSourcesList._expand_ppacCs@|jr2|jj|jd|gdd\}}}t|ƒdk}n | |¡}|S)NZexportT)Úcheck_rcr)r˜rrr†Ú_gpg_key_exists)r-Úkey_fingerprintrÚoutÚerrrˆrrrÚ_key_already_exists×s  z%UbuntuSourcesList._key_already_existsc sÆd}dg}tD]"‰| ‡fdd„t ˆ¡Dƒ¡q|D]Š}tj |¡r6z|j |jd|g¡\}}}WnJt t fy®}z.|  d|t |ƒf¡WYd}~q6WYd}~n d}~00||vr6d}qÂq6|S)NFz/etc/apt/trusted.gpgcs$g|]}| d¡stj ˆ|¡‘qS)Ú.)rSr'r(r:)rLÚx©Z other_dirrrrNçrOz5UbuntuSourcesList._gpg_key_exists..z--list-packetsz#Could check key against file %s: %sT) Ú APT_KEY_DIRSÚextendr'Úlistdirr(rvrrr™rqrnÚdebugr ) r-r°rˆZkeyfilesZkey_filerr±r²r|rr¶rr¯ás  "z!UbuntuSourcesList._gpg_key_existsrJc Cs| d¡rÌ| |¡\}}}||jvr*dS| ||¡}| |d¡s°d}|jjs°|jrt|jddddd|dg} nftD]} t j   | ¡rxq¤qx|j  d d   t¡¡d | t j  |¡ d d ¡||f}|jdddd|dg} |jj| ddd\} } } |r°t| ƒdkr|jj d| | | dzRt|dƒ}| | ¡Wdƒn1sH0Y|j d|d|f¡WnBttfy®}z$|jj d| | t|ƒdWYd}~n d}~00|pÈ| d||jf¡}n"|j|ddd}|pì| |¡}| |||¡dS)Núppa:Zsigning_key_fingerprintrJZadvz --recv-keysz--no-ttyz --keyserverzhkp://keyserver.ubuntu.com:80zNUnable to find any existing apt gpgp repo directories, tried the following: %sz, z%s/%s-%s-%s.gpgr>ú-z--exportT)r®Úencodingrz"Unable to get required signing key)rrÚstderrÚcommandÚwbz(Added repo key "%s" for apt to file "%s"z)Unable to add required signing key for%s )rrr¾Úerrorz%s_%srŠr‹)rSr­Ú repos_urlsr¨r³rrr˜r·r'r(rvrr:ÚbasenameÚreplacer™rr†r`rsr…rnrqr rRr”rXr‰)r-rPr6r.r5r¬r¦r§Zkeyfiler¿ZkeydirrÚstdoutr¾rdr|rrrrŒùs@    ÿ   *.zUbuntuSourcesList.add_sourcecCs:| d¡r| |¡d}n|j|ddd}| |¡dS)Nr»rTrŠr‹)rSr­rXrrrrrr‘+s zUbuntuSourcesList.remove_sourcec Cstg}|j ¡D]`}|D]V}|d}|d}|d}|r|st| ¡ƒ | ¡¡D]}tj |¡rt |¡q| ¡dS)z4Revert the sourcelist files to their previous state.N)r#ÚkeysÚ differencer'r(rvrwr})Úsources_beforeÚ sources_afterÚsourceslist_beforer;rrrÚrevert_sources_listGs  rÏcCsætttdddtddddgdtddtd dd gd td d dtd ddtddtd ddtd ddtddd dd}|j}|jd}|jd}|jd}d}tsZtr®dnd}tƒrÎ|jd |tj ¡dgd¢}t |dƒ}|rìt |ƒ|j r|jd|d|drt ||ƒn|jd|dt |dƒ}|rDt |ƒn|jd |tj ¡d|sl|jddtttjƒr„t|ƒ}n |jddt |¡} | ¡} z.|dkrº| |¡n|dkrÎ| |¡Wn:ty } z |jd t| ƒdWYd} ~ n d} ~ 00| ¡} | | k} | rŠ|jrŠg}t|  ¡ƒ |  ¡¡D]B}| |  |d!¡|  |d!¡|d"f|| v|d"f|| vd#œ¡qDni}| rÐ|j sÐzì|  ¡|r†d!}|j d$¡}|j d%¡}t! "d&d'¡d(}t#|ƒD]~}zt$ %¡}| &¡Wq†Wn2t$j'j(y2}zt|ƒ}WYd}~n d}~00d)||}||krR||}t) *|¡qàt+| | | ƒ|jd*|r||nd+dWnFt,t-fyÎ} z(t+| | | ƒ|jt| ƒdWYd} ~ n d} ~ 00|j.| |||d,dS)-NÚstrT)Útyper•ZpresentZabsent)rÑÚdefaultÚchoicesÚraw)rÑÚboolz update-cache)rÑrÒÚaliasesÚinté)rÑrÒé ) ÚrepoÚstaterkÚ update_cacheÚupdate_cache_retriesÚupdate_cache_retry_max_delayr;rZvalidate_certsr”)Z argument_specZsupports_check_moderÚrÛrÜz python3-aptz python-aptz+{0} must be installed and visible from {1}.r)z/usr/bin/python3z/usr/bin/python2z/usr/bin/pythonÚaptzX%s must be installed to use check mode. If run normally this module can auto-install it.rz4%s is not installed, and install_python_apt is Falsez/Please set argument 'repo' to a non-empty valuez1Module apt_repository is not supported on target.zInvalid repository string: %srJz /dev/null)ÚbeforeZafterZ before_headerZ after_headerrÝrÞrièg@@r‹zFailed to update apt cache: %szunknown reason)ÚchangedrÚrÛÚdiff)/rr¢r?ÚHAVE_PYTHON_APTr rrÚformatÚsysÚ executablerr rrÚ isinstancer–Úaptsources_distroZ Distributionr“ÚcopyÚdeepcopyrrŒr‘rr Z_diffr#rÊÚunionrbrtr}ÚrandomZrandintÚrangerßZCacher ÚcacheZFetchFailedExceptionÚtimeÚsleeprÏrnrqZ exit_json)rr?rÚrÛrÜZ sourceslistrZ interpretersZ interpreterrÎrÌrzrÍrárâr;r²rÝrÞZ randomizeZretryrîr|ZdelayrrrÚmainRs²     õ ò     ÿ           * ý        &rñÚ__main__)2Z __future__rrrrÑZ __metaclass__Z DOCUMENTATIONZEXAMPLESZRETURNrér+r£r'r@rårprìrïZansible.module_utils.basicrZ#ansible.module_utils.common.respawnrrr Zansible.module_utils._textr Zansible.module_utils.sixr Zansible.module_utils.urlsr rßrZZaptsources.distror–rèZ get_distrorãÚ ImportErrorr·rurKrÚ ExceptionrÚobjectr r“rÏrñrrrrrÚ sR\,      e