Jeff Hu Jeff Hu - 3 months ago 12
Bash Question

Automating tasks after vagrant ssh

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.

Answer

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