简体   繁体   中英

run python script on vagrant up

I'm trying to run a vagranfile that will result in a running python flask app. I've tried using this as the last command to achieve this - config.vm.provision :shell, :path => "python app.py" But it resulted with the following error -

* path for shell provisioner does not exist on the host system: /Users/*****/code/app-vagrant/python app.py

I understand that the script is trying to run this command from the host machine, how do I make Vagrant run the script on the vagrant machine launched?

you have 2 options to run a script using the vagrant shell provisioner you must pass either the inline or path argument:

inline (string) - Specifies a shell command inline to execute on the remote machine.

path (string) - Path to a shell script to upload and execute. It can be a script relative to the project Vagrantfile or a remote script (like a gist).

so when you pass :path => "python app.py" the system tries to find a script named python app.py on your host.

replace using inline argument and it will achieve what you want

config.vm.provision :shell, :inline => "python app.py"

note: provisioner are run as root user by default, if you want to change and run it as vagrant user:

config.vm.provision :shell, :inline => "python app.py", :privileged => false

If you want to start app when the machine start, you can do that:

  1. move your python app code directory to the Vagrantfile's directory

  2. Config the Vagrantfile like that:

     config.vm.provision "shell", inline: <<-SHELL python /vagrant/<your python app code directory>/app.py SHELL 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM