Vagrant ansible git 克隆权限错误

Vagrant ansible git clone permission error

我正在使用 vagrant 和 ansible provision。当我从 ansible 进行 git 克隆时,出现以下错误:

failed: [default] => {"cmd": "/usr/bin/git ls-remote '' -h refs/heads/HEAD", "failed": true, "rc": 128}
stderr: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

msg: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

FATAL: all hosts have already failed -- aborting

但是,每当我尝试手动从 vagrant box 克隆时,它都能正常工作。我搜索了网络,我在 vagrant 中将 ssh 转发设置为 true,我的 ~/.ssh/config 看起来像下面允许从主机转发的内容。

Host            *
  ForwardAgent  yes

我的ansible yml文件如下:

---
- hosts: all
  sudo: true
  tasks:
    - name: Clone project
      git: repo=<git ssh link>
           accept_hostkey=yes
           clone=yes
           dest=/home/vagrant

我的Vagrant文​​件如下:

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.network "forwarded_port", guest: 80, host: 9000
  config.vm.provision :ansible do |ansible|
    ansible.playbook = "playbook.yml"
  end
  config.ssh.forward_agent = true
end

每当我从 ansible 执行 git 克隆时,还有一个问题为什么它运行以下命令而不是 git 克隆:

/usr/bin/git ls-remote '' -h refs/heads/HEAD

我敢打赌 Ansible 不会使用您的配置进行 SSH 密钥转发(ForwardAgent 是)。

可能的建议解决方法是创建部署特定密钥,使用 Ansible 在部署目标中设置它们,然后使用这些密钥进行克隆。

例如