2

I can't force vagrant provisioning to clone private git repos from bitbucket. I have vagrant 1.6.3.

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.ssh.private_key_path = "~/.vagrant.d/insecure_private_key"
    config.ssh.forward_agent = true

    config.vm.define "abox" do |abox|
        abox.vm.box = "ubuntu/trusty32"
        abox.vm.hostname = "abox"
        abox.ssh.forward_agent = true

        abox.vm.network "private_network", ip: "192.168.50.4"
        abox.vm.network "forwarded_port", guest: 22, host: 2233
        abox.vm.network "forwarded_port", guest: 6340, host: 6340
        abox.vm.network "forwarded_port", guest: 8080, host: 6388

        abox.vm.provision :shell,
            :path => "provisioning/ssh_keys.sh", :privileged => false
        abox.vm.provision :shell,
            :path => "provisioning/setup_project.sh"
     end 
end

Where in ssh_keys I have:

function create_key() {
    ssh-add -L >> ~/.ssh/authorized_keys
    ssh-keyscan -t rsa 127.0.0.1 > ~/.ssh/known_hosts
}

create_key

Then in setup_project I call:

su - vagrant -c "ssh-keyscan bitbucket.org >> /home/vagrant/.ssh/known_hosts && \
                 ssh-keyscan github.com >> /home/vagrant/.ssh/known_hosts"

echo 'Clone bitbucket repo'
su - vagrant -c "cd /vagrant && git clone git@bitbucket.org:someuser/some-project-that-i-have-access-to.git"

The output is:

Permission denied (publickey).
==> abox: fatal: Could not read from remote repository.
==> abox: 
==> abox: Please make sure you have the correct access rights

==> abox: and the repository exists.

Error: Error while executing git clone -q git@bitbucket.org:someuser/some-project-that-i-have-access-to.git localclone

However when I vagrant ssh into the box and then call the same git clone command maually - everything works. i also tested ansible config but the problem was exactly the same.

What is wrong here?

eXt
  • 489
  • 5
  • 11

1 Answers1

4

The shell provisioning will run within the context of the guest machine (see docs).

Therefore you should just need to change setup_project to be (you may also want to remove key using ssh-keygen prior to cloning so you don't end up with duplicate records in ~/.ssh/known_hosts):

ssh-keygen -R bitbucket.org
ssh-keyscan bitbucket.org >> /home/vagrant/.ssh/known_hosts
ssh-keygen -R github.com
ssh-keyscan github.com >> /home/vagrant/.ssh/known_hosts

echo 'Clone bitbucket repo'

cd /vagrant
git clone git@bitbucket.org:someuser/some-project-that-i-have-access-to.git
jabclab
  • 14,786
  • 5
  • 54
  • 51
  • Can you explain this a little more? What I did was to: 1. remove `su - vagrant -c "command"` and only leave `command` 2. run the `setup_project` with :priviledged => false Now it works. I suppose that the issue here is related to the fact that initially I ran setup_project as root and `su` to user `vagrant`. – eXt Jun 20 '14 at 21:05