使用Ansible在本地系统上无法执行“git clone”

4

我想在本地系统上克隆一个git仓库。我已经手动完成了此操作并且运行良好,但是当我尝试使用Ansible执行时,它无法正常工作。

这是我的playbook:

---
  - name: Create a directory on root
    file:
      path: "{{ local_home_dir }}/superb-queue"
      owner: "{{ local_user }}"
      group: "{{ local_user }}"
      state: directory
    delegate_to: localhost

  - name: Clone the bitbucket queue repo locally
    git:
      repo: git@bitbucket.org:superbhq/queue-main.git
      dest: "{{ local_home_dir }}/superb-queue"
      clone: yes
      recursive: yes
      force: yes
      accept_hostkey: yes
      version: master
      key_file: "{{ local_home_dir }}/.ssh/id_rsa"
    become_user: "{{ local_user }}"
    delegate_to: localhost

The error I get is:

ASK [deploy-queue-main : Clone the bitbucket queue repo locally] ******************************************************************************************************************************************
fatal: [10.0.3.219 -> localhost]: FAILED! => {"changed": false, "cmd": "/usr/bin/git clone --origin origin '' /home/nishant/superb-queue", "msg": "fatal: destination path '/home/nishant/superb-queue' already exists and is not an empty directory.", "rc": 128, "stderr": "fatal: destination path '/home/nishant/superb-queue' already exists and is not an empty directory.\n", "stderr_lines": ["fatal: destination path '/home/nishant/superb-queue' already exists and is not an empty directory."], "stdout": "", "stdout_lines": []}
fatal: [10.0.4.36 -> localhost]: FAILED! => {"changed": false, "cmd": "/usr/bin/git clone --origin origin '' /home/nishant/superb-queue", "msg": "Cloning into '/home/nishant/superb-queue'...\nWarning:********@bitbucket.org: Permission 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 '/home/nishant/superb-queue'...\nWarning: Permanently added 'bitbucket.org,104.192.143.3' (RSA) to the list of known hosts.\r\ngit@bitbucket.org: Permission 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 '/home/nishant/superb-queue'...", "Warning: Permanently added 'bitbucket.org,104.192.143.3' (RSA) to the list of known hosts.", "git@bitbucket.org: 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": []}

本地系统上的目录为空,而我拥有正确的密钥。不确定为什么会出现这种情况。


2
“Here is my play” - 你发布的不是一个play,而是两个任务。在发布更多问题之前,请阅读如何创建最小、完整和可验证的示例。 - techraf
3个回答

10

您正在多个目标主机上并发运行任务,每个主机都委托给本地主机,它们之间实际上是相互竞争的:

fatal: [10.0.3.219 -> localhost]: ...
fatal: [10.0.4.36 -> localhost]: ...

在任务中添加run_once: true


2
我喜欢那个解释(比我的答案好)+1。 - VonC

5
你有两个错误:
  • path '/home/nishant/superb-queue' already exists and is not an empty directory
    如果你已经手动克隆了你的仓库,请确保在通过Ansible再次尝试之前删除该克隆存储库文件夹。
  • git@bitbucket.org: Permission denied (publickey).
    确保Ansible使用与您相同的帐户运行,以便在~/.ssh中使用相同的SSH密钥。
    或定义正确的ansible.cfg
问题5722中所提到的,请尝试:
- name: Clone code repository
  git:  repo=example.com/repos/enterprise.git
        dest=/home/user/enterprise
        accept_hostkey=yes
        force=yes
        recursive=no
        key_file={{ userhome }}/.ssh/id_rsa
        depth={{ repo_depth }}

这里,dest应该是克隆仓库的(不存在的)根文件夹(不是~,而是~/myrepo)。

对于点A:我正在克隆到一个新文件夹中,所以我有点困惑。 - Nishant Singh
@NishantSingh,为了测试,你可以尝试使用 force: yes 吗? - VonC
有没有强制选项,因为key_file的原因目录被克隆了,但是关于文件已存在的错误仍然存在。 - Nishant Singh
@NishantSingh 而且 /home/nishant/superb-queue 在启动 Ansible playbook 之前不存在吗?我不是指“为空”,而是指“不存在”。 - VonC
@NishantSingh 你能先试试不创建它吗? - VonC
显示剩余2条评论

0
从你的问题中,我假设你正在使用公共仓库,如果不是的话,那么你应该添加 key_file,并且你可能还需要添加一个用户。
请查看下面的解决方案以获取更多信息 https://dev59.com/LVwZ5IYBdhLWcg3wROmO#39735848 如果有帮助,请告诉我们。

是的,我已经添加了密钥文件并且它可以工作了。但是仍然出现了“msg”:“致命错误:目标路径'/home/nishant/superb-queue'已经存在且不是一个空目录。” - Nishant Singh
在克隆之前清空目录。在克隆过程中,目录需要为空。完成后,您就可以开始了。 - user9857025
尝试删除“superb-queue”并克隆“nishant”。如果可以,请告诉我。 - user9857025

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接