I want to be able to provision a vm by executing commands via ssh. I don't want to upload a shell script and execute it because that will not work on my case as my VM is a virtual appliance that has SSH support but not bash. Is this possible?
Thanks
Indeed you would not be able to use the shell provisioner with a script file.
a possibility is to use inline script
$script = <<SCRIPT
echo I am provisioning...
apt-get install -y apache2
SCRIPT
Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: $script
end
This should not upload any file but should execute the script directly from ssh
you can run vagrant ssh -c COMMAND to execute command from ssh once the machine is provisioned so not ideal but you could script all your provisioning with this command in a script and execute this script from host once the vm is boot
Related
I installed StackEdit on Vagrant. I would like to start Vagrant and StackEdit by one click.
I created bash script:
#!/bin/bash
vagrant up
#ssh -p 2222 -i /d/stackedit/.vagrant/machines/default/virtualbox/private_key vagrant#127.0.0.1 -t '/home/vagrant/Code/start_server.sh'
start "C:\Program Files\Mozilla Firefox\firefox.exe" http://stackedit.app:5000
and start_server.sh in VM
if [ $(ps -e|grep node|wc -l) = "0" ] ; then
(export PORT=5000 && node Code/Project/public/stackedit/server.js) &
fi
sleep 5
exit 0
If I run start_server.sh via ssh manualy everything works, but when I try it with ssh in start script - now commented line - server doesn't run.
I tried copy this script to /ect/rc.local, but the result is same.
I tried add #reboot /home/vagrant/Code/start_server.sh to crontab -e too, but without success.
Can anyone help me?
My system is Windows 10. I use Git Bash.
you should put everything in your Vagrantfile
#Run provisioning
You can run your script from Vagrantfile using a shell provisioner
Vagrant.configure("2") do |config|
config.vm.provision "shell", path: "Code/start_server.sh"
end
check, you have some options by default it will run as root so you can change if you want to run your script as vagrant user
Vagrant.configure("2") do |config|
config.vm.provision "shell", path: "Code/start_server.sh", privileged: false
end
and also you can make sure you run your script everytime you boot the VM (by default it runs only once or when calling specifically the provision parameter)
Vagrant.configure("2") do |config|
config.vm.provision "shell", path: "Code/start_server.sh", run: "always"
end
#opening the website after the system is running
Vagrantfile is a ruby script so you can call any command from the file, but it will run the command immediately and in any occasion.
Then, if you want to run after the box is started, you can use the vagrant trigger and do something like
Vagrant.configure(2) do |config|
.....
config.trigger.after :up do |trigger|
trigger.run = {inline: 'system("open", "http://stackedit.app:5000"')
end
end
Wondering if it is possible to automatically run a script or execute a command ONLY after vagrant ssh into the box? I understand that Ansible can provide beforehand installation and set up. But it failed to allow doing things automatically after entering the machine.
I am currently create a file script.sh. The file will be provided to the vagrant via Ansible. After I vagrant ssh into the box, I do bash script.sh to run the script. Is there better way?
Any suggestion would be more appreciated.
Two ways to achieve this,
Say assume your script is in vagrant home directory like,
:~$/home/vagrant/test-me.sh
1) Run command along with ssh
1a) vagrant ssh -- -t '/home/vagrant/test-me.sh; /bin/bash'
**-OR-**
1b) vagrant ssh -c '/home/vagrant/test-me.sh; /bin/bash'
2) Append complete script path in ~/.bashrc file (this should be in vagrant home directory if you are login as user vagrant)
:~$echo '. /home/vagrant/test-me.sh' >> ~/.bashrc
I'm on a Windows host using Git Bash to run the .sh files.
There are 4 components to my current project. To start up it on localhost, I have to:
webdriver-manager start since I'm the QA and need that running anyway
vagrant up in the project's parent folder, then close out that window (or just start the VM myself via VirtualBox UI)
vagrant ssh cd /vagrant cd "component's folder" docker-compose up x 4
grunt serve
Right now, I have a .sh file each for 1, 2, and 4, but I cannot find how to pass along multiple commands to vagrant ssh, especially since docker-compose up needs to be constantly running.
Is there a way to pass along those cds and the docker-compose?
I found the ssh documentation from vagrant which mentions something about needing to do fancy things to get it running background processes, but I have no idea what it's doing or how to implement that in a .sh file since the wording is so wishy-washy.
Also, I'm new to shell scripts in general, so if there's a smarter way to go about this to solve the issue, I'd appreciate it, too. These scripts aren't necessary, I just don't want to have to type it repeatedly every day when I'm running my tests locally.
From your Vagrantfile, have something like this
$script = <<SCRIPT
echo "running script in the VM"
cd /vagrant
cd "component's folder"
docker-compose up
cd "component's folder 2"
docker-compose up
# and add all other commands you would run from the VM
SCRIPT
Vagrant.configure(2) do |config|
....
config.vm.provision "shell", inline: $script
....
end
Note: this will run the commands as sudo (from your VM) if you want to run them as your vagrant user, just do
config.vm.provision "shell", inline: $script, privileged: "false"
If the commands needs to be invoked on vagrant up, you can provide provisioning script available on the host machine by:
config.vm.provision "shell", path: '/vagrant/scripts/provision.sh'
so Vagrant will then upload this script into the guest and execute it (using URL instead of path would also work),
Alternatively you may use inline shell syntax:
config.vm.provision "shell", inline: "echo Hello, World"
Or to run the script within VM, then try:
config.vm.provision "shell", inline: %Q(/usr/bin/env VAR=1 bash /vagrant/script.sh)
To run one-time off commands in VM, you may use vagrant ssh command for that, for example:
vagrant ssh -c "cd /vagrant && echo Hello, World"
Everytime I launch vagrant for one of our projects I go through the following incantation:
vagrant up
vagrant ssh
sudo su deploy
supervisorctl stop local
workon odoo-8.0
/home/deploy/odoo/build/8.0/openerp-server -c /home/deploy/odoo/local/odoo_serverrc
This runs the server in a way that lets me see the terminal output. Is there a way I could package this all up so I can do say; vagrant dev or some such?
You can use shell provisioner.
In your vagrantfile, you can do things like this:
$script = <<SCRIPT
echo I am provisioning...
date > /etc/vagrant_provisioned_at
SCRIPT
Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: $script
end
You can replace
echo I am provisioning...
date > /etc/vagrant_provisioned_at
with your own commands.
On the first 'vagrant up' that creates the environment, provisioning is run. If the environment was already created and the up is just resuming a machine or booting it up, they won't run unless the --provision flag is explicitly provided.
There are many more good ways to provision, I would also recommend using Ansible. Here is the doc you can read:
https://docs.vagrantup.com/v2/provisioning/basic_usage.html
First, create a shell script with your commands in them:
#!/bin/bash
vagrant up
vagrant ssh
sudo su deploy
supervisorctl stop local
workon odoo-8.0
/home/deploy/odoo/build/8.0/openerp-server -c /home/deploy/odoo/local/odoo_serverrc
Put it somewhere in your guest with ansible. Next, copy the /home/vagrant/.bashrc file into yoour ansible files/ folder. Add the line
bash /path/to/shellfile.sh
to the .bashrc and make sure ansible copies it into your guest.
After that, the shell script should be executed every time you log into the guest.
I'm asking so I don't have to "cd" everytime I use Vagrant. Thanks.
You can add cd dir-name to your .bashrc file inside your vm. So once you ssh into your vagrant machine it'll automatically run and change the directory.
On ubuntu .bashrc file is located in home (/home/vagrant) directory.
Alternatively you can connect to your vagrant box through starndard ssh command. This will allow you to specify the directory name at the connect time and have more freedom.
For example
ssh -p 2222 vagrant#localhost -t "cd dir-name ; /bin/bash"
You can see vagrant ssh config using below command. So you can check your port, user.. etc.
vagrant ssh-config