【问题标题】:Why is cloning a repo failing in Ansible but works manually?为什么在 Ansible 中克隆 repo 失败但手动工作?
【发布时间】:2021-09-28 03:44:06
【问题描述】:

作为更大剧本的一部分,我尝试在 Ansible 任务中克隆一个 repo,但它失败了 git@github.com: Permission denied (publickey).。 手动执行相同的操作。怎么了?

我的剧本的最小示例:

---
- hosts: all
  tasks:
    - name: Ensure dotfiles repository is cloned locally.
      git:
        repo: "git@github.com:geerlingguy/dotfiles.git"
        dest: "~/devops/macsetup/dotfiles"
        key_file: /Users/myUser/.ssh/id_ed25519
        version: "main"
        accept_hostkey: true
      become: false

库存文件:

[all]
127.0.0.1 ansible_connection=local

ansible.cfg:

[defaults]
nocows = true
roles_path = ./roles
inventory = inventory
become = true
stdout_callback = yaml
log_path = ~/Desktop/ansibleLog_test.txt

结果:

TASK [Ensure dotfiles repository is cloned locally.] ***************************************************************************************
The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
  File "/var/folders/3v/qk0n7z693jv2pz23cy99q7_m0000gn/T/ansible_git_payload_TeGqRk/ansible_git_payload.zip/ansible/modules/git.py", line 499, in clone
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
fatal: [127.0.0.1]: FAILED! => changed=false 
  cmd: /usr/bin/git ls-remote '' -h refs/heads/main
  invocation:
    module_args:
      accept_hostkey: true
      archive: null
      archive_prefix: null
      bare: false
      clone: true
      depth: null
      dest: /Users/myUser/devops/macsetup/dotfiles
      executable: null
      force: false
      gpg_whitelist: []
      key_file: /Users/myUser/.ssh/id_ed25519
      recursive: true
      reference: null
      refspec: null
      remote: origin
      repo: git@github.com:geerlingguy/dotfiles.git
      separate_git_dir: null
      single_branch: false
      ssh_opts: null
      track_submodules: false
      umask: null
      update: true
      verify_commit: false
      version: main
  msg: ''
  rc: 128
  stderr: |-
    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.
  stderr_lines: <omitted>
  stdout: ''
  stdout_lines: <omitted>
PLAY RECAP ***************************************************************************************
127.0.0.1                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

我意识到 ansible 实际上正在运行 git ls-remote '' -h refs/heads/main。 documentation on ls-remote 不太冗长。

【问题讨论】:

  • 您是否以同一用户身份运行 playbook 和手动测试?
  • 嗯,我想我知道。有没有办法记录这些信息?
  • 你的意思是当你发出两个命令时你不确定你登录的是哪个用户? (例如git clone ... vs ansible-playbook ...)
  • 我对此很确定,只是不确定 Ansible 是否以及何时将用户更改为 root 以在 ansible.cfg 中使用 become=true 进行操作。我刚刚尝试了 become=false 并得到了同样的错误......所以不是这样。
  • 那么当你在 Ansible 和手动执行命令时,你没有使用相同的键。

标签: git ansible


【解决方案1】:

如果没有更多信息,实际上不可能回答这个问题,但作为排除此类问题的一般规则 - 除了您已经完成的工作 - 以下是一些提示:

  • 在运行 ansible 时打开调试(例如,-vvvv 在命令行上) - 这通常可以帮助您查看额外信息,例如正在使用的用户/连接详细信息
  • 添加一些运行您在目标计算机上手动尝试过的命令行任务的预任务,并检查您是否看到相同的结果(例如 command: whoami 和 command: ls -l /Users/myUser/.ssh/id_ed25519)
  • 在目标上手动运行与通过 ansible 运行 - 通常情况下,手动登录是通过 dotfiles 设置 shell 环境,这可能与无头 ansible 登录不同

在后一种情况下,它取决于目标,但这里有一些 bash 示例:Not possible to source .bashrc with Ansible

【讨论】:

  • 好的,您的推荐结果: - 用户实际上是我 - 使用的密钥实际上是我个人的 ssh-private-key - 这台机器绝对是全新安装的 - 在任何点文件中都没有自定义
  • 你能添加/更新你正在测试的手动命令和你得到的输出吗?如果成功了,您是否必须尝试任何不同的方法,例如 docs.github.com/en/github/authenticating-to-github/… 上的建议?此外,在 ansible 中,您是否尝试通过 HTTP 而不是通过 SSH 密钥进行克隆?你得到同样的结果吗?
猜你喜欢
  • 1970-01-01
  • 2014-09-03
  • 2022-10-16
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 2022-09-29
  • 1970-01-01
相关资源
最近更新 更多