【发布时间】:2021-07-09 05:27:36
【问题描述】:
我一直在尝试让 Ansible 配置远程机器,并且我希望远程机器设置有自己的密钥,并且能够从 Bitbucket 克隆 git 存储库。
用户设置好了,有自己的id_rsa.pub,key已经注册到bitbucket了。
但是,当我使用 Ansible Git 模块时,该模块似乎总是尝试使用运行剧本的机器上的密钥。
如何让 git 模块使用远程机器上的 id_rsa.pub?
相关的任务是这样的:
- name: be sure prom-king has an up-to-date clone of its own repository
git:
repo: "ssh://ddcrnd@bitbucket.org/prom-king.git"
dest: /home/promking/prom-king
accept_hostkey: yes
clone: yes
key_file: /home/promking/.ssh/id_rsa.pub
update: yes
相关库存是这个
# inventory file for use with the vagrant box in the testing directory.
[prom-king]
192.168.168.192 ansible_ssh_host=127.0.0.1 ansible_sudo=true ansible_connection=ssh ansible_ssh_port=2222 ansible_ssh_user=vagrant ansible_ssh_private_key_file=testing/.vagrant/machines/default/virtualbox/private_key
【问题讨论】:
-
忘了提及...我可以让克隆与 https 一起使用(但这需要 url 中的密码,我想避免)并且当检索到该克隆时,文件都是由远程计算机上的 root 拥有 - 这也是不希望的。
-
粘贴到您正在使用的剧本中。可能只是 git 在远程机器上运行时提示将 bitbucket 添加到 known_hosts(是/否)提示,它在新机器上第一次运行时总是这样做
-
你怎么知道 git 使用你 playbook 机器上的密钥?所有 ansible 模块都在远程机器上运行,除非您明确在 127.0.0.1 上运行或将其标记为 local_action 或使用委托。
-
@Zasz。我已经在问题中添加了相关代码。我知道密钥是错误的,因为任务运行时出现密钥错误,并且这些 vagrant 密钥不在 bitbucket 帐户上。此外,当我 ssh 进入虚拟机并运行 git 命令时,它工作正常。我应该注意到,即使这是一个 vagrant vm,它也没有配置 ansible。在实际服务器上使用之前,我使用 VM 来测试剧本。 VM 使用私有网络,所以我像任何其他机器一样访问它(通过 IP 除外)最后,当我使用 https 时,它可以工作。该 repo 归 root 所有。
-
很确定您应该在 key_file 中指定私钥而不是公钥。
标签: git ssh ansible ssh-keys ansible-playbook