【问题标题】:How do I use remote machine's SSH keys in ansible git module如何在 ansible git 模块中使用远程机器的 SSH 密钥
【发布时间】: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


【解决方案1】:

这就是我使用远程服务器上设置的密钥文件从 Github 进行部署的方式。如果gitkeyfile 参数不起作用,则说明您的剧本有问题:

- name: Creates .ssh directory for root
  sudo: yes
  file: path=/root/.ssh state=directory

# This public key is set on Github repo Settings under "Deploy keys"
- name: Upload the private key used for Github cloning
  sudo: yes
  copy: src=keys/github dest=/root/.ssh/github

- name: Correct SSH deploy key permissions
  sudo: yes
  file: dest=/root/.ssh/github mode=0600

- name: Deploy site files from Github repository
  sudo: yes
  git:
    repo: git@github.com:miohtama/foobar.git
    dest: /srv/django/foobar
    key_file: /root/.ssh/github
    accept_hostkey: yes
    force: yes

【讨论】:

  • 如果密钥有密码,它会挂起。
  • 使用sudo 的方法现在显示[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default)
  • 我发现将上面的sudo: yesbecome_user: root 交换对我来说效果很好,并且删除了弃用通知。我已经在剧本的开头设置了becomebecome_method
  • 非常感谢。 accept_hostkey 部分在我不断收到的错误消息中并不明显。
【解决方案2】:

如果我理解正确,您确实 - 或想要 - 将您的私钥部署到远程机器,以便您可以克隆 repo。我相信你应该使用密钥转发。在您的.ssh/config 中设置:

ForwardAgent yes

或者,如果您想将其限制为 Ansible,您可以在 ansible.cfg 中定义它:

[ssh_connection]
ssh_args= -A

【讨论】:

  • 谢谢!以防万一,我改用ssh_args=-o ForwardAgent=yes 来消除一些讨厌的警告消息。
  • -o ForwardAgent=yes 和 -A 都不起作用。 -A 使警告出现
【解决方案3】:

一个有用的注释:对于使用 github 的任何人(我假设也适用于 gitlab 等) - 确保您以 SSH 形式正确提供 URL。如果提供了密钥文件但您提供了 HTTPS URL,它会悄悄地忽略密钥并(可能)挂起等待用户名和密码的输入。

【讨论】:

    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 2018-11-17
    • 2021-06-06
    相关资源
    最近更新 更多