【发布时间】:2022-01-09 05:46:52
【问题描述】:
我正在学习 Vagrant 和 Ansible,我正在尝试使用 Nginx 在 ubuntu20.04 中为基本的烧瓶应用程序设置本地开发环境。
我的 vagrantfile 看起来像这样:
Vagrant.configure("2") do |config|
config.vm.define :ubuntuserver do | ubuntuserver |
ubuntuserver.vm.box = "bento/ubuntu-20.04"
ubuntuserver.vm.hostname = "ubuntuserver"
ubuntuserver.vm.provision :ansible do | ansible |
ansible.playbook = "development.yml"
end
ubuntuserver.vm.network "private_network", ip:"10.11.1.105"
ubuntuserver.vm.network "forwarded_port", guest: 80, host: 8080
ubuntuserver.vm.network "public_network", bridge: "en1: Wi-Fi (AirPort)"
ubuntuserver.vm.provider :virtualbox do |vb|
vb.memory = "1024"
end
ubuntuserver.vm.synced_folder "./shared", "/var/www"
end
end
我的 ansible-playbook 是这样的:
-
name: local env
hosts: ubuntuserver
tasks:
- name: update and upgrade apt packages
become: yes
apt:
upgrade: yes
update_cache: yes
- name: install software properties common
apt:
name: software-properties-common
state: present
- name: install nginx
become: yes
apt:
name: nginx
state: present
update_cache: yes
- name: ufw allow http
become: yes
community.general.ufw:
rule: allow
name: "Nginx HTTP"
- name: installing packages for python env
become: yes
apt:
name:
- python3-pip
- python3-dev
- build-essential
- libssl-dev
- libffi-dev
- python3-setuptools
- python3-venv
update_cache: yes
- name: Create app directory if it does not exist
ansible.builtin.file:
path: /var/www/app
state: directory
mode: '0774'
- name: Install virtualenv via pip
become: yes
pip:
name: virtualenv
executable: pip3
- name: Set python virual env
command:
cmd: virtualenv /var/www/app/ -p python3
creates: "/var/www/app/"
- name: Install requirements
pip:
requirements: /var/www/requirements.txt
virtualenv: /var/www/app/appenv
virtualenv_python: python3
我的剧本在下一个任务中失败并出现错误:
- name: Activate /var/www/app/appenv
become: yes
command: source /var/www/app/appenv/bin/activate
fatal: [ubuntuserver]: FAILED! => {"changed": false, "cmd": "source /var/www/app/appenv/bin/activate", "msg": "[Errno 2] No such file or directory: b'source'", "rc": 2}
剧本的其余部分
- name: ufw allow 5000
become: yes
community.general.ufw:
rule: allow
to_port: 5000
- name: Run app
command: python3 /var/www/app/appenv/app.py
根据我对this 线程的了解,“source”命令必须在 vagrant 机器内部使用。 (我尝试了线程中的解决方案,但无法使其正常工作) 如果我 ssh 进入 vagrant 机器并手动执行我的剧本的最后三个命令:
source /var/www/app/appenv/bin/activate
sudo ufw allow 5000
python3 /var/www/app/appenv/app.py
我的基本烧瓶应用程序在 vagrantfile 10.11.1.105 中设置的 IP 的 5000 端口上运行
我的问题是:
我怎样才能让 playbook 工作而不必通过 ssh 进入机器来完成同样的工作?
我的方法是否正确,我知道我的最终目标是在 vagrant 机器中复制与生产环境类似的环境,并在 synced folder 的本地机器上开发烧瓶应用程序?
提供最大的信息,如果有人想重现这一点。 我还有一个包含基本烧瓶应用程序的 shared/app/appenv/app.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')
和 shared/requirements.txt 文件
wheel
uwsgi
flask
【问题讨论】:
标签: ansible vagrant virtualenv