簡體   English   中英

如何使用 Ansible 在 virtualenv 上安裝 python 包?

[英]How to Install python packages on a virtualenv using Ansible?

我無法啟動 gunicorn 我收到此錯誤

錯誤:

fatal: [172.105.102.110]: FAILED! => {
   "changed":false,
   "cmd":"/myproject/myprojectenv/bin/gunicorn -D --chdir /myproject --error-logfile /root/.ansible/tmp/ansible-tmp-1593463703.788082-353660-248038870081082/gunicorn.temp.error.log --pid /root/.ansible/tmp/ansible-tmp-1593463703.788082-353660-248038870081082/gunicorn.temp.pid wsgi",
   "msg":"[Errno 2] No such file or directory: b'/myproject/myprojectenv/bin/gunicorn'",
   "rc":2
}

do_tutorial.yml

---
- hosts: DigitialOceanExample
  become: yes
  tasks: 
  - name: Update apt-get repo and cache
    apt: 
      update_cache: yes 
      force_apt_get: yes 
      cache_valid_time: 3600

  - name: Install a list of packages
    apt:
      pkg:
      - python3-pip
      - python3-dev
      - build-essential
      - libssl-dev
      - libffi-dev
      - python3-setuptools
      - python3-venv

  - name: ensure a directory exists or create it
    file: 
      path: /myproject
      state: directory

  - name: Manually create the initial virtualenv
    command:
      cmd: python3 -m venv /myproject/myprojectenv
      creates: "/myproject/myprojectenv"

  - name: "install python packages with the local instance of pip"
    shell: "pip3 install wheel flask gunicorn"

  - name: copy file to server
    copy: 
      src: "{{ item }}"
      dest: /myproject
    loop:
      - ./myproject.py

  - name: Install ufw
    apt:
      name: ufw
      update_cache: true

  - name: "Allow port 5000"
    shell:  "ufw allow 5000"

  - name: copy file to server
    copy: 
      src: ./wsgi.py
      dest: /myproject 

  # - name: "starting gunicorn"
  #   shell: "gunicorn --bind 0.0.0.0:5000 wsgi:app"

  - name: run gunicorn on a virtualenv
    gunicorn:
      app: 'wsgi'
      chdir: '/myproject'
      venv: '/myproject/myprojectenv'


  

我的項目.py

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

if __name__ == "__main__":
    app.run(host='0.0.0.0')

wsgi.py

from myproject import app

if __name__ == "__main__":
    app.run()

主機

[DigitialOceanExample]
PPP.PPP.PPP.PPP (redacted for StackOverFlow question)

命令

ansible-playbook -i inventory  do_tutorial.yml 

我正在嘗試使用 Ansible 復制本教程,但我的 virtualenv 出現錯誤

我認為您正在使用全局 pip 而不是在虛擬環境中進行安裝。 嘗試在新創建的 venv 中使用 pip3 的絕對路徑:

  - name: "install python packages with the local instance of pip"
    shell: "/myproject/myprojectenv/bin/pip3 install wheel flask gunicorn"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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