python - 如何使用 Ansible 在 virtualenv 上安装 python 包?
问题描述
我无法启动 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"
推荐阅读
- python - 如何使用 np.where() 将数组元素划分为类别?
- swift - 如何在 MacOS 上使用 Swift & AVMutableComposition 和 AVPlayer 以半速播放 60fps 视频
- javascript - 定位特定的 div 类而不是 nextElementSibling
- amazon-web-services - AWS - Fargate 任务的私有静态 IP 地址
- c# - 如何制作不同数据类型的列表然后保存到文件c#
- amazon-web-services - 创建 AWS 预算操作以自动停止 EC2 和 RDS 时出错
- c - 命令挂在管道的读取端
- c++ - 如何避免在 C++ 中使用 new 运算符?
- actions-on-google - 对 Google 采取行动 - 在禁止交易中消费
- sql - 表 3 优于表 2,表 2 优于表 1