Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 967
Jeff Hu Avatar asked Jun 08 '26 12:06

Jeff Hu


1 Answers

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
like image 177
Balaji Reddy Avatar answered Jun 10 '26 02:06

Balaji Reddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!