【问题标题】:ansible git clone Permission denied issueansible git clone 权限被拒绝问题
【发布时间】:2015-12-25 14:40:10
【问题描述】:

我正在尝试使用 ansible 和 vagrant 设置开发环境。现在我被困在从 bitbucket 克隆一个 git repo。

下面是 Vagrantfile 和 ansible 设置以及我尝试过的东西。

在我的 Vagrantfile 中,我有 config.ssh.forward_agent = true

在我的 deploy.yml 我有

- name: ensure bitbucket is a known host
  lineinfile:
    dest: /home/vagrant/.ssh/known_hosts
    create: yes
    state: present
    line: "{{ lookup('pipe', 'ssh-keyscan -p 443 -t rsa altssh.bitbucket.org') }}"
    regexp: "^altssh\\.bitbucket\\.org"
  sudo_user: "vagrant"
  tags: known_hosts

- name: checkout opentest repo
  git: >
    dest="/home/vagrant/ot/"
    repo="ssh://git@altssh.bitbucket.org:443/muhammadammar/opentest/"
    version="master"
    accept_hostkey=yes
  sudo_user: "vagrant"
  tags: checkout

上面的第一个任务将bitbucket的公钥添加到/home/vagrant/.ssh/known_hosts

下面是 ansible 尝试检查 repo 时的输出

<127.0.0.1> ESTABLISH CONNECTION FOR USER: vagrant
<127.0.0.1> REMOTE_MODULE git dest="/home/vagrant/ot/" repo="ssh:********@altssh.bitbucket.org:443/muhammadammar/opentest/" version=master accept_hostkey=yes
<127.0.0.1> EXEC ssh -C -vvv -o ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/Users/ammar/.ansible/cp/%h-%r" -o StrictHostKeyChecking=no -o Port=2222 -o IdentityFile="/Users/ammar/.vagrant.d/insecure_private_key" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 127.0.0.1 /bin/sh -c 'sudo -k && sudo -HE -S -p "[sudo via ansible, key=vwkfotuvzzmllxvxptkgehjzdlqygoez] password: " -u vagrant /bin/sh -c '"'"'echo BECOME-SUCCESS-vwkfotuvzzmllxvxptkgehjzdlqygoez; LANG=C LC_CTYPE=C /usr/bin/python'"'"''
failed: [default] => {"cmd": "/usr/bin/git ls-remote 'ssh:********@altssh.bitbucket.org:443/muhammadammar/opentest/' -h refs/heads/master", "failed": true, "rc": 128}
stderr: RSA host key for IP address '131.103.20.174' not in list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

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

msg: RSA host key for IP address '131.103.20.174' not in list of known hosts.
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

           to retry, use: --limit @/Users/ammar/dev.retry

default                    : ok=1    changed=0    unreachable=0    failed=1   

Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.

在我的主机上,我已将我的私钥添加到 ssh 身份验证代理。我可以在主机和 vagrant box 上使用 ssh-add -l 查看私钥身份。

在 vagrant box 中,我可以使用 git clone ssh://git@altssh.bitbucket.org:443/muhammadammar/opentest ot 成功克隆 repo

我不确定是什么问题。谁能帮忙解决这个问题。

编辑

即使是下面的也不能在 ansible 中工作

shell: "git clone ssh://git@altssh.bitbucket.org:443/muhammadammar/opentest/ /home/vagrant/omp/"

【问题讨论】:

标签: git ssh vagrant ansible


【解决方案1】:

能够在 vagrant box 上成功运行 git clone ... 是不够的。

在我看来,正在发生的事情是:

  • ansible 尝试以用户 'vagrant' (User=vagrant) 的身份打开与 vagrant box 的 ssh 连接并提供身份文件:-o IdentityFile="/Users/ammar/.vagrant.d/insecure_private_key"
  • 执行/bin/sh -c 'sudo -k &amp;&amp; sudo -HE -S -p "[sudo via ansible, key=vwkfotuvzzmllxvxptkgehjzdlqygoez] password: " -u vagrant /bin/sh -c '"'"'echo BECOME-SUCCESS-vwkfotuvzzmllxvxptkgehjzdlqygoez; LANG=C LC_CTYPE=C /usr/bin/python'"'"'
  • 尝试使用用户“********”git clone (ssh:********@altssh.bitbucket.org:443/...)

你应该也能做到,你是吗?

如果不是,那么原因可能很多,例如

  • /Users/ammar/.vagrant.d/insecure_private_key 不是用户 vagrant 的正确文件。
  • vagrant 不是 sudoer。
  • vagrant@vagrant 用户的密钥未在 ********@altssh.bitbucket.org 注册

查看上述所有步骤的日志/输出后,您就会知道正确的原因。

PS:当您已经在使用同一个用户进行 ssh 时,使用 sudo_user: vagrant 似乎是多余的。我可以想到一些可能会这样做的原因,但至少在这里它是多余的。

【讨论】:

  • 感谢您的回复,但不幸的是我不知道上面的任何内容,因为这是我第一次研究 ansible 和 vagrant。我从没想过为 git 操作正确配置 vagrant box 很复杂,需要了解很多设置。
【解决方案2】:

我遇到了同样的问题。您是否在脚本中使用“sudo:是”?如果是,那么 root 用户需要访问 repo,而不是 vagrant 用户。

将root用户的公钥文件添加到Bitbucket账号中就可以了。

您也可以通过普通用户进行结帐。

无论如何,在 vagrant 中访问 repo 的用户的公钥需要能够访问 Bitbucket。

【讨论】:

    猜你喜欢
    • 2021-11-11
    • 2011-12-15
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    相关资源
    最近更新 更多