簡體   English   中英

如何使用 Ansible 在服務器上安裝 Python 版本

[英]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-latestgithub鏈接 )。

首先要考慮的事情是,你可能希望更換或升級的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 ,它可以使用許多不同的來源,並使用它們來制作debrpm等。 它還可以使用僅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


以下詳細介紹了我所做的一些事情。 我並不認為這是最佳實踐,但它確實顯示了我如何選擇分離這些問題:


ancible.cfg

#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。

變量.yml

在不同的變量中跟蹤應用程序Python 版本。

# application, not system/Ansible, python
py_appver: "3.10" #2️⃣
py_app_bin: "/usr/bin/python{{py_appver}}"

我很少在劇本中使用py_appver

在 VM 上啟動 Python 狀態:

(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

劇本.yml:

添加apt的存儲庫以從以下位置獲取 3.10:

    - name: add Python dead snakes repo for 3.10
      ansible.builtin.apt_repository:
        repo: 'ppa:deadsnakes/ppa'

安裝Python3.10等一些包

    ##############################################
    # 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️⃣

我沒有做的事情:符號鏈接 python3.10 -> python3

    # 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 

我如何將 3.10 用於我的虛擬環境:

同樣,這可能不一定是最佳實踐,但它確實有效。

結果是使用 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。

最后在 VM 上的/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

激活應用程序 Python:

(ssha)vagrant bin$source /srv/venv/bin/activate # 4️⃣
(venv) (ssha)vagrant bin$python --version # 2️⃣
Python 3.10.1

環境

  • 主機 Macos BigSur,Python 3.10
    • 流浪者 2.2.19
    • 虛擬機 6.1.30,148432
$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)]
  • 來賓 Ubuntu 20.04 和 Python 3.8

不確定來賓端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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM