o nnh @sddlmZdZdZdZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZdZdZdZz ddlmZd ZWn'eyidZe ZzddlmZejZ[d ZWn eyfYnwYnwdd lmZdd lmZmZmZdd l m!Z!d ddddddZ"e#dZ$e j%e j&e j'e j(e j)e j*e j%dZ+ddZ,ddZ-ddZ.ddZ/ddZ0dd Z1d!d"Z2d2d#d$Z3d%d&Z4d'd(Z5d3d)d*Z6d+d,Z7Gd-d.d.Z8d/d0Z9e:d1kre9dSdS)4) annotationsa --- module: pip short_description: Manages Python library dependencies description: - "Manage Python library dependencies. To use this module, one of the following keys is required: O(name) or O(requirements)." version_added: "0.7" options: name: description: - The name of a Python library to install or the url(bzr+,hg+,git+,svn+) of the remote package. - This can be a list (since 2.2) and contain version specifiers (since 2.7). type: list elements: str version: description: - The version number to install of the Python library specified in the O(name) parameter. type: str requirements: description: - The path to a pip requirements file, which should be local to the remote system. File can be specified as a relative path if using the chdir option. type: str virtualenv: description: - An optional path to a I(virtualenv) directory to install into. It cannot be specified together with the 'executable' parameter (added in 2.1). If the virtualenv does not exist, it will be created before installing packages. The optional virtualenv_site_packages, virtualenv_command, and virtualenv_python options affect the creation of the virtualenv. type: path virtualenv_site_packages: description: - Whether the virtual environment will inherit packages from the global site-packages directory. Note that if this setting is changed on an already existing virtual environment it will not have any effect, the environment must be deleted and newly created. type: bool default: "no" version_added: "1.0" virtualenv_command: description: - The command or a pathname to the command to create the virtual environment with. For example V(pyvenv), V(virtualenv), V(virtualenv2), V(~/bin/virtualenv), V(/usr/local/bin/virtualenv). type: path default: virtualenv version_added: "1.1" virtualenv_python: description: - The Python executable used for creating the virtual environment. For example V(python3.12), V(python2.7). When not specified, the Python version used to run the ansible module is used. This parameter should not be used when O(virtualenv_command) is using V(pyvenv) or the C(-m venv) module. type: str version_added: "2.0" state: description: - The state of module - The 'forcereinstall' option is only available in Ansible 2.1 and above. type: str choices: [ absent, forcereinstall, latest, present ] default: present extra_args: description: - Extra arguments passed to pip. type: str version_added: "1.0" editable: description: - Pass the editable flag. type: bool default: 'no' version_added: "2.0" chdir: description: - cd into this directory before running the command type: path version_added: "1.3" executable: description: - The explicit executable or pathname for the pip executable, if different from the Ansible Python interpreter. For example V(pip3.3), if there are both Python 2.7 and 3.3 installations in the system and you want to run pip for the Python 3.3 installation. - Mutually exclusive with O(virtualenv) (added in 2.1). - Does not affect the Ansible Python interpreter. - The setuptools package must be installed for both the Ansible Python interpreter and for the version of Python specified by this option. type: path version_added: "1.3" umask: description: - The system umask to apply before installing the pip package. This is useful, for example, when installing on systems that have a very restrictive umask by default (e.g., "0077") and you want to pip install packages which are to be used by all users. Note that this requires you to specify desired umask mode as an octal string, (e.g., "0022"). type: str version_added: "2.1" break_system_packages: description: - Allow pip to modify an externally-managed Python installation as defined by PEP 668. - This is typically required when installing packages outside a virtual environment on modern systems. type: bool default: false version_added: "2.17" extends_documentation_fragment: - action_common_attributes attributes: check_mode: support: full diff_mode: support: none platform: platforms: posix notes: - Python installations marked externally-managed (as defined by PEP668) cannot be updated by pip versions >= 23.0.1 without the use of a virtual environment or setting the O(break_system_packages) option. - The virtualenv (U(http://www.virtualenv.org/)) must be installed on the remote host if the virtualenv parameter is specified and the virtualenv needs to be created. - Although it executes using the Ansible Python interpreter, the pip module shells out to run the actual pip command, so it can use any pip version you specify with O(executable). By default, it uses the pip version for the Ansible Python interpreter. For example, pip3 on python 3, and pip2 or pip on python 2. - The interpreter used by Ansible (see R(ansible_python_interpreter, ansible_python_interpreter)) requires the setuptools package, regardless of the version of pip set with the O(executable) option. requirements: - pip - virtualenv - setuptools or packaging author: - Matt Wright (@mattupstate) aD - name: Install bottle python package ansible.builtin.pip: name: bottle - name: Install bottle python package on version 0.11 ansible.builtin.pip: name: bottle==0.11 - name: Install bottle python package with version specifiers ansible.builtin.pip: name: bottle>0.10,<0.20,!=0.11 - name: Install multi python packages with version specifiers ansible.builtin.pip: name: - django>1.11.0,<1.12.0 - bottle>0.10,<0.20,!=0.11 - name: Install python package using a proxy ansible.builtin.pip: name: six environment: http_proxy: 'http://127.0.0.1:8080' https_proxy: 'https://127.0.0.1:8080' # You do not have to supply '-e' option in extra_args - name: Install MyApp using one of the remote protocols (bzr+,hg+,git+,svn+) ansible.builtin.pip: name: svn+http://myrepo/svn/MyApp#egg=MyApp - name: Install MyApp using one of the remote protocols (bzr+,hg+,git+) ansible.builtin.pip: name: git+http://myrepo/app/MyApp - name: Install MyApp from local tarball ansible.builtin.pip: name: file:///path/to/MyApp.tar.gz - name: Install bottle into the specified (virtualenv), inheriting none of the globally installed modules ansible.builtin.pip: name: bottle virtualenv: /my_app/venv - name: Install bottle into the specified (virtualenv), inheriting globally installed modules ansible.builtin.pip: name: bottle virtualenv: /my_app/venv virtualenv_site_packages: yes - name: Install bottle into the specified (virtualenv), using Python 2.7 ansible.builtin.pip: name: bottle virtualenv: /my_app/venv virtualenv_command: virtualenv-2.7 - name: Install bottle within a user home directory ansible.builtin.pip: name: bottle extra_args: --user - name: Install specified python requirements ansible.builtin.pip: requirements: /my_app/requirements.txt - name: Install specified python requirements in indicated (virtualenv) ansible.builtin.pip: requirements: /my_app/requirements.txt virtualenv: /my_app/venv - name: Install specified python requirements and custom Index URL ansible.builtin.pip: requirements: /my_app/requirements.txt extra_args: -i https://example.com/pypi/simple - name: Install specified python requirements offline from a local directory with downloaded packages ansible.builtin.pip: requirements: /my_app/requirements.txt extra_args: "--no-index --find-links=file:///my_downloaded_packages_dir" - name: Install bottle for Python 3.3 specifically, using the 'pip3.3' executable ansible.builtin.pip: name: bottle executable: pip3.3 - name: Install bottle, forcing reinstallation if it's already installed ansible.builtin.pip: name: bottle state: forcereinstall - name: Install bottle while ensuring the umask is 0022 (to ensure other users can use it) ansible.builtin.pip: name: bottle umask: "0022" become: True - name: Run a module inside a virtual environment block: - name: Ensure the virtual environment exists pip: name: psutil virtualenv: "{{ venv_dir }}" # On Debian-based systems the correct python*-venv package must be installed to use the `venv` module. virtualenv_command: "{{ ansible_python_interpreter }} -m venv" - name: Run a module inside the virtual environment wait_for: port: 22 vars: # Alternatively, use a block to affect multiple tasks, or use set_fact to affect the remainder of the playbook. ansible_python_interpreter: "{{ venv_python }}" vars: venv_dir: /tmp/pick-a-better-venv-path venv_python: "{{ venv_dir }}/bin/python" a cmd: description: pip command used by the module returned: success type: str sample: pip2 install ansible six name: description: list of python modules targeted by pip returned: success type: list sample: ['ansible', 'six'] requirements: description: Path to the requirements file returned: success, if a requirements file was provided type: str sample: "/srv/git/project/requirements.txt" version: description: Version of the package specified in 'name' returned: success, if a name and version were provided type: str sample: "2.5.1" virtualenv: description: Path to the virtualenv returned: success, if a virtualenv path was provided type: str sample: "/tmp/virtualenv" N) LooseVersionF) RequirementT to_native) AnsibleModule is_executablemissing_required_lib)get_best_parsable_localezDfrom importlib.metadata import version; print(version("setuptools"))z=from importlib.metadata import version; print(version("pip")) setuptoolspipz0import setuptools; print(setuptools.__version__)zJimport pkg_resources; print(pkg_resources.get_distribution("pip").version)) importlib pkg_resourcesz(svn|git|hg|bzr)\+)z>=z<=><==z!=z~=cCs tt|S)z(Test whether a name is a vcs url or not.)rematch_VCS_REnamer>/usr/local/lib/python3.10/dist-packages/ansible/modules/pip.py _is_vcs_url]s rcCsXt}|jdtdt|}|ddkrdS||dd\}}|jdkr*dSdS) N-mtyperpyvenvTvenvF)argparseArgumentParser add_argumentstrshlexsplitparse_known_argsm)command venv_parserargvargsdummyrrr_is_venv_commandbs   r.cCs|tt S)z?Test whether the name is a package name or a version specifier.)lstrip startswithtupleop_dictkeysrrrr_is_package_namensr4cCsg}|D] }||dq|}g}g}d}|D]'}t|r-|s-|r+|d|g}d|vr3d}|r;d|vr;d}||q|d||S)aRecover package names as list from user's raw input. :input: a mixed and invalid list of names or version specifiers :return: a list of valid package name eg. input: ['django>1.11.1', '<1.11.3', 'ipaddress', 'simpleproject>1.1.0', '<2.0.0'] return: ['django>1.11.1,<1.11.3', 'ipaddress', 'simpleproject>1.1.0,<2.0.0'] input: ['django>1.11.1,<1.11.3,ipaddress', 'simpleproject>1.1.0,<2.0.0'] return: ['django>1.11.1,<1.11.3', 'ipaddress', 'simpleproject>1.1.0,<2.0.0'] ,F[T])extendr&r4appendjoin)namestmpone_line name_parts package_names in_bracketsrrrr_recover_package_namess&   rAcCsV|d}||\}}}|dkr|jd|||fd|}dd|D}|S)Nz --helprz Could not get output from %s: %smsgcSsg|] }|dr|qS)z--r0).0xrrr sz$_get_cmd_options..) run_command fail_jsonstripr&)modulecmdthiscmdrcstdoutstderrwords cmd_optionsrrr_get_cmd_optionss rSc Cs|ddg}t|}|||d}|j|||d\}}}|dkr9|dg}|j||d\}}}|dkr9t||||d|||fS) z.Return results of pip command to get packages.listz--format=freeze)LANGLC_ALL LC_MESSAGES)cwdenviron_updaterfreezerX )r rH_failr:) rKr chdirr)localelang_envrNouterrrrr _get_packagess   rccCsL|D]!}d|vr|d\}}t|}nq||jkr#||r#dSqdS)z+Return whether or not package is installed.rTF)r&Packagecanonicalize_name package_nameis_satisfied_by)rKreqinstalled_pkgs pkg_commandpkgpkg_name pkg_versionrrr _is_presents rnc Cs d}d}|durtj|r|}n|f}n|dur&|dur&tr&tjddg}|dur|durNg}|D]}||d|}|durAn q2|jdd|dn8tj|d}|d d f}|D]}tj||}tj |rtt |rt|}nq]|jd |d d|d dt |t s|g}|S)N)pip3rz pip.__main__Fzs  z*Package.is_satisfied_by..)rrrcontainsAttributeErrorrallr)rrrrrrgs   zPackage.is_satisfied_bycCstjd|S)N-)rd_CANONICALIZE_REsublowerrrrrreszPackage.canonicalize_namecCs|jrt|jS|jSr)rrrrfrrrr__str__s zPackage.__str__r)__name__ __module__ __qualname____doc__rcompilerrpropertyrrg staticmethodrerrrrrrdis     rdc"Cstdgddgddggdd}tttddt|d td dd tdd tdd td d tdddtd ddtdd tdd tdddtd d td d tdd tddddddggddgddggdd}tsrtsr|jtdtd|j d}|j d}|j d}|j d}|j d}|j d}|j d}|j d} d} | r|rt j || } |rt |tszt|d}Wnty|jd ttd!d"Ynwd} |durt |} z|d#kr|dur|jd$d%|durt}d&} d&} | rt j t j | d'd(sd} t|| || | \} } t j | d'd)}n |j dp tj}t|| |j d}|||}d}| r|vs#d?|vrDd@D]}||vrBt(|||}|durB| || dA|7} q%|D]}t)||||}|dkrW|r_|dBkrc|rcd}nqF|j$||| | dCd}|sv|rt&|||\}}}|j*|||dD\} }}| |7} | |7} | d!kr|dBkrdE|vsdE|vrn | d-krt+||| | |dBkrdF|v}n|durdG|v}n t&|||\}}!}||!k}|p| }|j$||||||| | | dH W| durt | dSdS| durt | ww)INinstall uninstallz-y-U)rrz--force-reinstall)presentabsentlatestforcereinstallr$r)rdefaultchoicesrT)relementsrrrrF)rr virtualenv)staterversion requirementsrrrr extra_argseditabler^rvumaskbreak_system_packagesrrrvT) argument_specrequired_one_ofmutually_exclusivesupports_check_mode packaging)rC exceptionrrrr^rzumask must be an octal integerr)rCdetailsrz)version is incompatible with state=latestrBrrpactivatepythoncSsg|]}t|qSr)rd)rErkrrrrG szmain..z'version' argument is ambiguous when installing multiple package distributions. Please specify version restrictions next to each package in 'name' argument.rzThe 'version' argument conflicts with any version specifier provided along with a package name. Please keep the version specifier, but remove the 'version' argument.rr\z-er1PIP_BREAK_SYSTEM_PACKAGEScss|]}t|VqdSrrrEprrrr.szmain..z-rz)No valid name or requirements file found.)rwarningsrcSs(g|]}|ds|ds|r|qS)z You are usingzYou should considerrDrrrrrGBs( z freezer r r z%s r)rrLrOrP) path_prefixrXz not installedzSuccessfully uninstalledzSuccessfully installed) rrLrrrrrrOrP),dictrrTr3HAS_SETUPTOOLS HAS_PACKAGINGrIr PACKAGING_IMP_ERRrrqrrr:ryintrrruexc_infortempfile gettempdirrxrrvrrrAlenrrdr&r9r8r%environrrrcendswithrrnrHr])" state_maprKrrrrrr^rrz venv_created old_umaskrbrapy_binr rLrhas_vcsrkpackages args_listpkg_cmdout_piperr_piprpkg_listrr is_presentout_freeze_beforer-rNout_freeze_afterrrrmains>                                              r__main__)NNr); __future__r DOCUMENTATIONEXAMPLESRETURNr!rqrruroperatorr% traceback#ansible.module_utils.compat.versionrrrrpackaging.requirementsrrr format_excrparser+ansible.module_utils.common.text.convertersransible.module_utils.basicrrr "ansible.module_utils.common.localer rrrgelegtlteqner2rr.r4rArSrcrnrrtr]rrrdrrrrrrs u         %  3 3?O