Using Sed to Append Lines to File During Vagrant Provisioning
To fix "sed: -e expression #1, char 44: extra characters after command", escape \n, that is add another backslash to all \n:
config.vm.provision "shell", inline:
"sudo sed -i \'$ a if [ -d \\\"/usr/local/lib\\\" ] then\\n PATH=\\\"/usr/local/lib:$PATH\\\"\\nfi\' /home/vagrant/.profile"
why doesn't vagrant provisioner modify ~/.bashrc?
I think what's happening is the provisioning script is run as root (or sudo), so the "~" home location is actually /root rather than the default user home location /home/vagrant.
I can think of a couple ways to solve this:
First (and probably easiest) is to be explicit about the .bashrc path, like:
# Vagrantfile
Vagrant.configure("2") do |config|
... various cmds to set box and network...
$install_user_vars = <<SCRIPT
sudo cat /vagrant/dev_env_config >> /home/vagrant/.bashrc
echo "*** here is the .bashrc file:"
cat /home/vagrant/.bashrc
SCRIPT
config.vm.provision "shell", inline: $install_user_vars
end
The second option could be to run this part of the provisioning script as a non-privileged user. See the 'privileged' option on the Shell Scripts docs page.
Vagrant: Is it possible to share files and alter permissions inside the guest?
Im not sure if this quite answers your question, but I had issue with my applications not being able to write to the disk on a shared folder system so I used the following in my Vagrantfile
to allow r/w access to any shared files from within the client.
config.vm.synced_folder "./vhosts/", "/var/www/vhosts/", id: "vagrant-root", extra: "dmode=777,fmode=777"
How do I add my own public key to Vagrant VM?
Copying the desired public key would fall squarely into the provisioning phase. The exact answer depends on what provisioning you fancy to use (shell, Chef, Puppet etc). The most trivial would be a file
provisioner for the key, something along this:
config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/me.pub"
Well, actually you need to append to authorized_keys. Use the the shell provisioner, like so:
Vagrant.configure(2) do |config|
# ... other config
config.vm.provision "shell", inline: <<-SHELL
cat /home/vagrant/.ssh/me.pub >> /home/vagrant/.ssh/authorized_keys
SHELL
# ... other config
end
You can also use a true provisioner, like Puppet. For example see Managing SSH Authorized Keys with Puppet.
How to ssh to vagrant without actually running vagrant ssh?
I've had to re-implement "vagrant ssh" because it's -c
option didn't pass on arguments properly. This is basically what it does (there might be more, but it works fine this way)
#!/bin/sh
PORT=$(vagrant ssh-config | grep Port | grep -o '[0-9]\+')
ssh -q \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
-i ~/.vagrant.d/insecure_private_key \
vagrant@localhost \
-p $PORT \
"$@"
As a one-liner (with thanks to kgadek):
ssh $(vagrant ssh-config | awk 'NR>1 {print " -o "$1"="$2}') localhost
To account for when you have more than one vagrant host, this will select the desired host, as well as cull blank lines from the config (using sed):
HOST=name-of-my-host
ssh $(vagrant ssh-config $HOST | sed '/^[[:space:]]*$/d' | awk 'NR>1 {print " -o "$1"="$2}') localhost
Related Topics
Check If a Table Exists in Rails
Resize Existing Images to New Style in Paperclip & Rmagick
What Is a Very Simple Authentication Scheme for Sinatra/Rack
Writing a Pre-Push Hook in Git to Grep All Files for Regex Want to Reject Push If Regex Not Found
How to Sort a Ruby Array by Two Conditions
How to Verify Pacts Against an API That Requires an Auth Token
Rspec Error 'Report_Activate_Error': Could Not Find Rubygem Rspec-Core (>=0) (Gem:Loaderror)
Does Ruby Provide a Constant_Added Hook Method
Mongomapper Association Skips Duplicates
Rally Ruby Toolkit: How to Get Url of Portfolio Item's State
Scope That Has Three Levels Deep Joins
Iterating Over the Registers of a Yardoc '@Macro'
Watir Won't Download PDF, Only Opens in Viewer
Chef: Undefined Node Attribute or Method '<<' on 'Node' When Trying to Add