如何在 ansible git 模块中使用远程机器的 SSH 密钥



我一直在尝试让 Ansible 配置远程机器,我希望使用自己的密钥设置远程机器,并能够从 Bitbucket 克隆 git 存储库。

用户已设置,拥有自己的 id_rsa.pub,并且密钥已向 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

这就是我使用远程服务器上设置的密钥文件从 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

如果我理解正确,您确实或想要将私钥部署到远程计算机,以便克隆存储库。我认为您应该使用密钥转发。在您的.ssh/config中设置以下内容:

ForwardAgent yes

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

[ssh_connection]
ssh_args= -A
<</div> div class="one_answers">

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

最新更新