【问题标题】:Ansible git clone github permission denied (publickey)Ansible git clone github 权限被拒绝(公钥)
【发布时间】:2021-11-11 00:47:40
【问题描述】:

我已经为此工作了大约 2 个小时,但我完全不知所措。我知道以前有人问过这个问题并回答过这个问题,但我完全被难住了。

我正在尝试通过 ansible 将 github 存储库克隆到虚拟机上。

当我这样做时,我收到以下错误:

Warning: Identity file /home/REMOTE_USER/.ssh/id_ed25519 not accessible: No such file or directory
git@github.com: Permission denied (publickey)
fatal: Could not read from remote repository
Please make sure you have the correct access rights and the repository exists.

这是处理 github 克隆的 ansible playbook:


- name: Create SSH Key
  hosts: vm
  remote_user: REMOTE_USER

  vars:
    local_keyfile: /home/LOCAL_USER/.ssh/id_ed25519
    public_key: "{{ lookup('file', '/home/LOCAL_USER/.ssh/id_ed25519.pub') }}"
    remote_keyfile: /home/REMOTE_USER/.ssh/id_ed25519
    repository: "git@github.com:GITHUB_USERNAME/GITHUB_REPO.git"

  tasks:

  - name: Add public key to authorized keys
    lineinfile:
      path: /home/REMOTE_USER/.ssh/authorized_keys
      line: "{{ public_key }}"

  - name: Copy SSH Key
    copy:
      src: "{{ local_keyfile }}"
      dest: "{{ remote_keyfile }}"
      owner: REMOTE_USER
      group: REMOTE_USER
      mode: 0600

  - name: Configure SSH to use ansible key for github.com
    template:
      src: templates/ssh_config.j2
      dest: /home/REMOTE_USER/.ssh/config
      owner: REMOTE_USER
      group: REMOTE_USER
      mode: 0644

  - name: Clone repo
    git:
      dest: /home/REMOTE_USER/PATH_TO_REPO
      repo: "{{ repository }}"
      key_file: " {{ remote_keyfile }}"
      clone: yes
      update: yes

我为尝试解决此问题而采取的步骤:

  1. 将公钥添加到远程主机上的 authorized_keys
  2. 已添加
Host github.com
   IdentityFile PATH_TO_PRIVATE_KEY
   IdentitiesOnly yes

到远程主机上的 .ssh/config 文件。

  1. 创建了一个 ansible.cfg 文件,其中包含:
[defaults]
transport = ssh
sudo_flags = -HE

[ssh_connection]
ssh_args = -o ForwardAgent=yes
  1. root & REMOTE_USER 运行剧本
  2. 删除了Copy SSH Key 任务

我已经搞砸了所有这些设置,并且没有混合使用。如果我手动通过 SSH 连接到虚拟机,我可以运行 git clone git@github.com:GITHUB_USER/REPO,它工作正常(但会提示我输入 SSH 密钥的密码)

REMOTE_USER 拥有私钥、repo 被克隆到的目录、authorized_keys、公钥和配置文件的权限。

我从 stackoverflow、其他 stackexchange 网站和一般互联网上读到的所有内容到目前为止都没有用。

对于如何解决此问题(不涉及将 HTTPS 用于 git 克隆)的任何指导,我将不胜感激。

【问题讨论】:

    标签: git github ssh ansible


    【解决方案1】:

    我能够在本地重现您的问题,我发现问题是字符串中的前导空格

          key_file: " {{ remote_keyfile }}"
    

    如果您删除第一个引号后的空格,它应该可以工作。

    我应该注意我在本地测试时没有在密钥上设置密码。我想密码可能是git 模块的问题。

    【讨论】:

    • 谢谢,我明天早上试试!
    • 这成功了!我确实必须使用没有密码的密钥,并且能够执行克隆: 1. 无需将我的公钥复制到 authorized_keys 2. 无需设置 ansible.cfg 3. 无需将密钥路径添加到 .ssh/config在远程主机上(适用于将来可能遇到此问题的任何人)
    猜你喜欢
    • 2015-12-25
    • 2011-11-25
    • 1970-01-01
    • 2012-01-18
    • 2012-04-25
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多