使用 Ansible 从 Bitbucket 克隆一个 git 存储库 - 要求密码两次或三次

Cloning a git repo from Bitbucket with Ansible - asked for password two or three times

我正在尝试使用 Ansible 1.9.3 (OSX) 和 https 连接从 Bitbucket 克隆私有 git 存储库。我将密码存储在剪贴板中,并在要求提供时使用粘贴。以下命令要求我提供密码两次或三次(不定期,从不一次且不超过三次):

[~/devops]# ansible localhost -c local -m git -a "repo=https://techraf@bitbucket.org/techraf/ansible-local.git dest=~/devops/ansible-local"
Password for 'https://techraf@bitbucket.org':
Password for 'https://techraf@bitbucket.org':
Password for 'https://techraf@bitbucket.org':
localhost | success >> {
    "after": "445dfaf39a6245bc30149dd722b1a17d0e56ba55",
    "before": null,
    "changed": true
}

[~/devops]#

在任何一次尝试中提供不正确的密码都会立即导致错误 remote: Invalid username or password,因此输入错误是不可能的。 -vvv 选项没有给出提示。延迟输入密码似乎不会影响行为。

为什么我被问了好几次,为什么次数不同?

Ansible git 模块不只是克隆。它还可以更新现有的本地存储库、使用子模块等。(http://docs.ansible.com/ansible/git_module.html)

我的猜测是它正在执行多个操作,其中每个操作都需要访问远程 BitBucket 存储库。查看 git module's source code 表明,即使只是 clone 步骤,它也会使用不同的参数执行 git 二进制文件几次。此处可能会发生这种情况 - 取决于您是否已经克隆了存储库,命令的数量可能会有所不同,并且与本地存储库交互的每个命令都会再次要求输入密码。

要解决这个问题,您应该考虑在目标机器上设置 Git credential helper。在最简单的情况下,您可以使用 cache 实现,它会将您的密码缓存几分钟。在这种情况下输入一次就足够了。