如何使用主机的 SSH 密钥在 Vagrant VM 中 git clone private repos?

ape*_*ker 11 ssh git puppet vagrant

我可以使用 PuppetLabsvcsrepo来克隆公共 git 存储库,但我也希望能够使用主机的 SSH 密钥克隆私有存储库。

为了实现这一点Vagrantfile和/或manifests/default.pp为了实现这一点,配置会是什么样子?

小智 10

Puppet 部分我无能为力,但您可以通过设置转发 SSH 代理:

Vagrant.configure("2") do |config|
  config.ssh.forward_agent = true
  # ...
end
Run Code Online (Sandbox Code Playgroud)

这样 SSH 连接(也由 git 建立)尝试使用来自主机的私钥。


ape*_*ker 5

在我的机器上工作!

流浪文件:

VAGRANTFILE_API_VERSION = '2'

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = 'precise64'
  config.vm.box_url = 'http://files.vagrantup.com/precise64.box'

  #
  # Use host authenticaton for git and maven.
  #
  # Ensure host private key is registered with host SSH agent:
  #
  # ssh-add -L
  # ssh-add ~/.ssh/id_rsa
  # ssh-add -L
  #

  config.ssh.private_key_path = ['~/.vagrant.d/insecure_private_key', '~/.ssh/id_rsa']
  config.ssh.forward_agent = true

  config.vm.synced_folder "~/.m2", "/home/vagrant/.m2"

  config.vm.provision :shell, path: 'upgrade-puppet.sh'

  # Install puppet modules
  config.vm.provision :shell, path: 'bootstrap.rb', args: %w(
    puppetlabs-stdlib
    puppetlabs/apt
    puppetlabs/vcsrepo
  )

  config.vm.provision :puppet do |puppet|
    puppet.options = ENV['PUPPET_OPTIONS']
  end
end
Run Code Online (Sandbox Code Playgroud)

升级-puppet.sh:

#!/bin/bash

apt-get install --yes lsb-release > /dev/null
DISTRIB_CODENAME=$(lsb_release --codename --short)
DEB="puppetlabs-release-${DISTRIB_CODENAME}.deb"
DEB_PROVIDES="/etc/apt/sources.list.d/puppetlabs.list" # Assume that this file's existence means we have the Puppet Labs repo added

if [ ! -e $DEB_PROVIDES ]
then
    # Print statement useful for debugging, but automated runs of this will interpret any output as an error
    # print "Could not find $DEB_PROVIDES - fetching and installing $DEB"
    wget -q http://apt.puppetlabs.com/$DEB
    sudo dpkg -i $DEB
fi
sudo apt-get update > /dev/null
sudo apt-get install --yes puppet > /dev/null

mkdir -p /etc/puppet
touch /etc/puppet/hiera.yaml
Run Code Online (Sandbox Code Playgroud)

引导程序.sh:

#!/usr/bin/env ruby

modules_dir = '/etc/puppet/modules'

puts `mkdir -p #{modules_dir}` unless File::exists? modules_dir

mods = ARGV

installed = `puppet module list`.split "\n"

mods.each do |mod|
  puts `puppet module install #{mod}` unless installed.any? { |i| i.include?(mod.sub('/','-')) }
end
Run Code Online (Sandbox Code Playgroud)

清单/default.pp:

exec { 'ssh know github':
  command => 'ssh -Tv git@github.com -o StrictHostKeyChecking=no; echo Success',
  path    => '/bin:/usr/bin',
  user    => 'vagrant'
}

vcsrepo { '/home/vagrant/a-private-repo':
  ensure   => latest,
  provider => git,
  source   => 'git@github.com:mcandre/a-private-repo.git',
  user     => 'vagrant',
  owner    => 'vagrant',
  group    => 'vagrant',
  require  => Exec['ssh know github']
}
Run Code Online (Sandbox Code Playgroud)