Vagrantゲスト同士でSSHできるようにする
経緯
WindowsでAnsibleを使いたかったので、VagrantでAnsible用VMを立ててそこからplaybookを流す
vagrant-host-shellを導入
$ vagrant plugin install vagrant-host-shell
insert_keyをfalseにする
Vagrant.configure("2") do |config|
.......
box = "ubuntu/trusty64"
vagrant.vm.box = box
config.ssh.insert_key = false
vagrant.vm.network "private_network", ip: "192.168.33.254"
# IPを設定しておく
.........
scpでAnsible用ゲストOSにprivate_keyをぶん投げる
config.vm.provision :host_shell do |host_shell|
host_shell.inline = 'scp -i ~/.vagrant.d/insecure_private_key -o "StrictHostKeyChecking no" ~/.vagrant.d/insecure_private_key vagrant@192.168.33.254:/home/vagrant/.ssh/id_rsa'
end
vagrant.vm.provision "shell", inline: <<-SHELL
chmod 600 /home/vagrant/.ssh/id_rsa
SHELL
end
所感
手動でぶち込むのは避けられたはず... まだWindowsで動作確認はしていないが、insecure_private_keyの場所が違うくらいしか引っかかる場所は無い気がする
追記
Windowsで試したところ、scpがうまくいかなかった(cygwin使用)
ので、cpでvagrantフォルダ内に移動させてmv&chmod。
config.vm.provision :host_shell do |host_shell|
host_shell.inline = 'cp ../../.vagrant.d/insecure_private_key ./insecure_private_key'
end
vagrant.vm.provision "shell", inline: <<-SHELL
set -e
mv /vagrant/insecure_private_key /home/vagrant/.ssh/id_rsa
chmod 600 /home/vagrant/.ssh/id_rsa
SHELL
end