![](/img/trans.png)
[英]How to Install python packages on a virtualenv using Ansible?
[英]How to install a Python version on server using Ansible
我正在使用 ansible 连接服务器。 但是由于 python 的旧版本,我收到某些 pip 包的错误。 如何使用 ansible 安装特定版本的 python ( 2.7.10
)。 服务器上当前的python版本是2.7.6
现在我已经手动编译并安装了 python 版本,但更希望有一种方法通过 ansible 来完成。
除了@Simon Fraser的答案外,以下剧本是我在Ansible中使用的用来准备具有某些特定Python 3版本的服务器的内容:
# python_version is a given variable, eg. `3.5`
- name: Check if python is already latest
command: python3 --version
register: python_version_result
failed_when: "{{ python_version_result.stdout | replace('Python ', '') | version_compare(python_version, '>=') }}"
- name: Install prerequisites
apt: name=python-software-properties state=present
become: true
- name: Add deadsnakes repo
apt_repository: repo="ppa:deadsnakes/ppa"
become: true
- name: Install python
apt: name="python{{ python_version }}-dev" state=present
become: true
如果您对此感兴趣,我也具有上述角色,称为ansible-python-latest ( github链接 )。
首先要考虑的事情是,你可能不希望更换或升级的Python的系统版本。 这是因为系统本身将它用于软件包管理之类的事情,因此替换它可能会导致其他重要事情中断。
安装其他人制作的Python额外副本
要安装额外版本的Python,最简单的选择是使用ppa ,例如https://launchpad.net/~fkrull/+archive/ubuntu/deadsnakes-python2.7,以便其他人可以完成将Python变成适合您的软件包。
PPA可以使用以下指令与Ansible的apt仓库模块一起添加PPA,然后该指令将允许您以正常的ansible方式从中安装软件包:
apt_repository: repo='ppa:fkrull/deadsnakes-python2.7'
自己构建一个包
如果没有具有所需Python版本的ppa
,则可能需要自己构建.deb
软件包。 最简单的方法是使用诸如checkinstall之类的工具。 还有fpm ,它可以使用许多不同的来源,并使用它们来制作deb
, rpm
等。 它还可以使用仅pip install
可用的Python模块,然后将其转换为系统软件包,这非常有用。
有了deb
软件包后,就可以与Ansible的apt模块一起安装
apt: deb=/tmp/mypackage.deb
我想指出,Ansible 构建中可能涉及 2 或 3 个不同的 Python,因此不要将它们混在一起是很有用的。
1️⃣ 系统/Ansible Python。 在我的 VirtualBox 来宾 Ubuntu 20.04 上,现在是 3.8.x。
2️⃣ 您的应用程序/生产Python,包括通过 Ansible 执行的pip/venv
任务。 就我而言,我自己的应用程序代码使用 3.10。
3️⃣ 您在主机上使用的开发,而不是系统,Python,这又是一个单独的问题。 (在我的例子中,macos,Python 3.10)。
您希望将应用程序的开发 3️⃣ 和生产2️⃣ Python 保持在同等水平。
然而,Ansible 不需要使用 3.10,因此我将不理会 Ansible 和系统 Python。
值得一提的是,我的主机 Macbook 也在运行 Python 3.10,但这不会影响来宾使用 3.8
以下详细介绍了我所做的一些事情。 我并不认为这是最佳实践,但它确实显示了我如何选择分离这些问题:
#dont start out with 3.10, because it may not exist yet
# ansible_python_interpreter=/usr/bin/python3.10
ansible_python_interpreter=/usr/bin/python3 #1️⃣
我没有在我的剧本中调整ansible_python_interpreter
,即 Ansible 留下了 20.04 交付的 Python。
在不同的变量中跟踪应用程序Python 版本。
# application, not system/Ansible, python
py_appver: "3.10" #2️⃣
py_app_bin: "/usr/bin/python{{py_appver}}"
我很少在剧本中使用py_appver
。
(ssha)vagrant bin$pwd
/usr/bin
(ssha)vagrant bin$ls -l python3*
lrwxrwxrwx 1 root root 9 Mar 13 2020 python3 -> python3.8
(ssha)vagrant bin$/usr/bin/python3 --version #1️⃣
Python 3.8.10
apt
的存储库以从以下位置获取 3.10: - name: add Python dead snakes repo for 3.10
ansible.builtin.apt_repository:
repo: 'ppa:deadsnakes/ppa'
##############################################
# These utilities may be used by any of the tasks
# so might as well put them in early
##############################################
- name: install system-level components
package: "name={{ item }} state=present"
with_items:
- monit
- runit
....
# needed by ANXS.postgresql
- python3-psycopg2
# not sure I needed but..
- python3-pip
#Application Python
- python{{py_appver}} #2️⃣
- python{{py_appver}}-venv #2️⃣
# DONT DO THIS
# - name: symlink python executables
# file:
# src: "/usr/bin/{{item.from_}}{{pyver}}"
# dest: "/usr/bin/{{item.to_}}"
# state: link
# force: true
# with_items:
# - {from_: "python", to_: "python3"}
# - {from_: "pyvenv-", to_: "pyvenv"}
# when: false
同样,这可能不一定是最佳实践,但它确实有效。
结果是使用 Python 3.10 拥有/srv/venv
4️⃣ virtualenv
- name: create virtualenv manually
command: "{{py_app_bin}} -m venv ./venv" #2️⃣
args:
chdir: "/srv"
become: yes
become_user: builder
when: not venv_exists.stat.exists
现在,我要求pip
自我更新和安装东西:
- name: pip self-update to 20.x
pip:
name: pip
state: latest
virtualenv: "/srv/venv" # 4️⃣
- name: pip requirements 1st pass
pip:
requirements: "{{ dir_app }}/requirements.txt"
virtualenv: "/srv/venv" # 4️⃣
virtualenv_python: "python{{py_appver}}" #2️⃣
就是这样。 我的 pip/venv 东西可以使用 3.10,而其他所有东西,包括 ansible,都使用 3.8。
/usr/bin
中有什么:(ssha)vagrant bin$pwd
/usr/bin
(ssha)vagrant bin$ls -l python3*
lrwxrwxrwx 1 root root 9 Mar 13 2020 python3 -> python3.8
-rwxr-xr-x 1 root root 5454104 Dec 21 09:46 python3.10
-rwxr-xr-x 1 root root 5490488 Nov 26 12:14 python3.8
lrwxrwxrwx 1 root root 33 Nov 26 12:14 python3.8-config -> x86_64-linux-gnu-python3.8-config
lrwxrwxrwx 1 root root 16 Mar 13 2020 python3-config -> python3.8-config
(ssha)vagrant bin$python3 --version # 1️⃣
Python 3.8.10
(ssha)vagrant bin$source /srv/venv/bin/activate # 4️⃣
(venv) (ssha)vagrant bin$python --version # 2️⃣
Python 3.10.1
$ansible --version
ansible [core 2.12.1]
config file = /Users/myuser/.ansible.cfg
configured module search path = ['/Users/myuser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /Users/myuser/kds2/venvs/bme/lib/python3.10/site-packages/ansible
ansible collection location = /Users/myuser/.ansible/collections:/usr/share/ansible/collections
executable location = /Users/myuser/kds2/venvs/bme/bin/ansible
python version = 3.10.1 (main, Dec 10 2021, 12:10:01) [Clang 12.0.5 (clang-1205.0.22.11)]
不确定来宾端ansible的相关性如何,但无论如何我都会添加它:
$apt list | egrep -i ^ansible
ansible-doc/focal 2.9.6+dfsg-1 all
ansible-lint/focal 4.2.0-1 all
ansible-tower-cli-doc/focal 3.3.0-1.1 all
ansible-tower-cli/focal 3.3.0-1.1 all
ansible/focal 2.9.6+dfsg-1 all
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.