Vagrant provision shell:无法运行ssh-copy-id

时间:2017-09-06 08:02:11

标签: vagrant ansible devops

我有一个sinple Vagrant文件,它创建了3个实例:

config.vm.define "node1" do |subconfig|
    subconfig.vm.box = "ubuntu/trusty64"
subconfig.vm.network "private_network", ip: "192.168.33.10"
end

config.vm.define "node2" do |subconfig|
    subconfig.vm.box = "ubuntu/trusty64"
subconfig.vm.network "private_network", ip: "192.168.33.11"
end

 config.vm.define "master" do |subconfig|
     subconfig.vm.box = "ubuntu/trusty64"
subconfig.vm.network "private_network", ip: "192.168.33.9"
subconfig.vm.provision :shell, path: "install.sh"
end

在主节点上,我想生成ssh-key,然后将其复制到其他2个节点。我的配置shell(install.sh):

apt-get install sshpass -y
ssh-keygen -t rsa -b 4096 -C "tuananh93nguyen@gmail.com" -N "" -f /home/vagrant/.ssh/id_rsa
sudo chmod -R 755 /home/vagrant/.ssh
sshpass -p vagrant ssh-copy-id -o StrictHostKeyChecking=no vagrant@192.168.33.10
sshpass -p vagrant ssh-copy-id -o StrictHostKeyChecking=no vagrant@192.168.33.11

但是在最后一行运行时我遇到了这个错误:

> master: /usr/bin/ssh-copy-id: ERROR: No identities found

但是,如果我ssh到主节点,然后再次运行install.sh,一切都很好。我认为这可能是关于在流浪者上运行时的用户权限。我该如何解决?

1 个答案:

答案 0 :(得分:0)

您遇到的问题是,您正在使用root用户运行脚本,但是推入了vagrant文​​件夹,因此您会感到很困惑。

在Vagrantfile脚本中添加privileged: false

subconfig.vm.provision :shell, path: "install.sh", privileged: false

所以它将与vagrant用户而不是root用户一起运行,然后您不需要更改密钥的权限(删除sudo chmod -R 755 /home/vagrant/.ssh

小注意:如果你对所有3台机器使用相同的盒子,你可以只有一个公共变量并删除subconfig.vm.box = "ubuntu/trusty64"的所有行:

config.vm.box = "ubuntu/trusty64"

config.vm.define "node1" do |subconfig|
    subconfig.vm.network "private_network", ip: "192.168.33.10" 
end
...