【问题标题】:Ansible bitbucket clone repo provisioning ssh errorAnsible bitbucket 克隆 repo 配置 ssh 错误
【发布时间】:2017-12-13 02:12:22
【问题描述】:

我以前发布过这个问题,但那里的答案不再有效。

总之,当我使用 Ansible 配置我的 vagrant box 时,我在尝试使用 ssh 克隆我的 bitbucket 私有 repo 时遇到了一个神秘的错误。错误状态为“权限被拒绝(公钥)”。

然而,如果我 vagrant ssh 然后运行 ​​'git clone' 命令,则私有 repo 被成功克隆。这表明 ssh 转发代理确实在工作,并且 vagrant box 可以访问我与 bitbucket repo 关联的私钥。

我已经在这个问题上苦苦挣扎了两天,我失去了理智!请有人帮帮我!!!

流浪文件:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64"
  config.vm.network "private_network", ip: "192.168.33.14"
  config.ssh.forward_agent = true

  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1824"
  end

  # Only contains ansible dependencies
  config.vm.provision "shell",
    inline: "sudo apt-get install python-minimal -y"

end

我的playbook.yml如下:

---

- hosts: all
  become: true

  tasks:
    - name: create /var/www/ directory
      file: dest=/var/www/ state=directory owner=ubuntu group=www-data mode=0755

    - name: Add the user 'ubuntu' to group 'www-data'
      user:
        name: ubuntu
        shell: /bin/bash
        groups: www-data
        append: yes

    - name: Clone [My-Repo] bitbucket repo
      become: false
      git: 
        repo: git@bitbucket.org:[Username]/[My-Repo].com.git
        dest: /var/www/poo
        version: master
        accept_hostkey: yes

错误信息: ansible-playbook playbook.yml

fatal: [192.168.33.14]: FAILED! => {"changed": false, "cmd": "/usr/bin/git clone --origin origin '' /var/www/poo", "failed": true, "msg": "Cloning into '/var/www/poo'...\nPermission denied (publickey).\r\nfatal: Could not read from remote repository.\n\nPlease make sure you have the correct access rights\nand the repository exists.", "rc": 128, "stderr": "Cloning into '/var/www/poo'...\nPermission denied (publickey).\r\nfatal: Could not read from remote repository.\n\nPlease make sure you have the correct access rights\nand the repository exists.\n", "stderr_lines": ["Cloning into '/var/www/poo'...", "Permission denied (publickey).", "fatal: Could not read from remote repository.", "", "Please make sure you have the correct access rights", "and the repository exists."], "stdout": "", "stdout_lines": []}

其他信息:

  • 我机器上的 ssh-add -l 确实包含相关的 bitbucket 存储库密钥。
  • ssh-add -l vagrant box 内也包含相关的 bitbucket repo 密钥(通过 ssh-forwarding)。

如果在 vagrant box 中手动完成,克隆仍然有效

vagrant ssh
git clone git@bitbucket.org:myusername/myprivaterepo.com.git
Then type "yes" to allow the RSA fingerprint to be added to ~/.ssh/known_hosts (as its first connection with bitbucket)

非常感谢任何帮助,感谢您阅读我的噩梦。

【问题讨论】:

  • 我没有看到“主机密钥验证”失败,但确实看到了 Permission denied (publickey) 错误。也许不要发布重复的问题,而是尝试与人们就您的原始问题进行交流。
  • 这不是重复问题,而是一个新问题,因为之前的问题已通过在 playbook.yml 中包含“become: false”行来解决。这是我创建的另一个线程中不存在的新错误消息。请删除重复状态。谢谢。

标签: git ssh vagrant ansible bitbucket


【解决方案1】:

这通常意味着 Ansible 不会尝试使用与使用 vagrant ssh 的用户相同的用户克隆 repo。

更好地调试正在发生的事情的一个技巧是运行命令:

GIT_SSH_COMMAND='ssh -v' git clone ...

这样,您将确切地看到尝试了哪些 ssh 密钥。

正如kostix 建议in the comments,在Ansible 命令中添加id(或id -a)也会有所帮助。


OP Gustavmahler 确认in the comments

您是对的:Ansible 将 repo 克隆为与我预期不同的用户。
我添加了以下内容来修复任务:

become: true 
become_user: vagrant 

【讨论】:

  • @kostix 好点。我已将您的评论包含在答案中以提高知名度。
  • 感谢 VonC - 你是对的,ansible 将 repo 克隆为与我预期不同的用户。我添加了以下内容来修复任务:
  • become: true become_user: vagrant
  • @GustavMahler 谢谢。我已将您的评论包含在答案中以提高知名度。
【解决方案2】:

ssh-agent 与终端会话相关联 - 但自动 Ansible 运行不是。 (对于大多数 cron 作业来说也是如此,fwiw。)这也解释了为什么如果你 SSH 到你的 Vagrant 盒子并运行它,事情就会正常工作。

如果您将ansible_ssh_private_key_file: /path/to/file 添加到剧本中,那么是否可以解决问题?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2018-10-12
    • 2013-08-07
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    相关资源
    最近更新 更多