Ansible 中通过 SSH 的 GIT 挂起,即使设置了 ssh-agent 转发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21427577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:38:54  来源:igfitidea点击:

GIT over SSH in Ansible hangs, eventhough ssh-agent forwarding is set up

gitsshtimeoutansiblessh-agent

提问by tillda

I have set up everyhing I could find, but still cloning a repo from GitHub hangs the provisioning process.

我已经设置了我能找到的所有东西,但仍然从 GitHub 克隆一个 repo 挂起配置过程。

I have:

我有:

  • server in known_hosts
  • .ssh/config

    Host github.com
      ForwardAgent yes
      StrictHostKeyChecking no
    
  • copied private key

  • public key is in authorized_keys
  • the command runs as vagrantuser
  • the play is:

    - name: Checkout from git
      git: [email protected]:username/repositoryname.git dest=/srv/website
    
  • known_hosts 中的服务器
  • .ssh/配置

    Host github.com
      ForwardAgent yes
      StrictHostKeyChecking no
    
  • 复制的私钥

  • 公钥在authorized_keys中
  • 命令以vagrant用户身份运行
  • 该剧是:

    - name: Checkout from git
      git: [email protected]:username/repositoryname.git dest=/srv/website
    

回答by Tom Seldon

Just to expand on tillda's answer, that config can be placed in an ansible.cfg file alongside your playbook. e.g.:

只是为了扩展tilda的答案,该配置可以放置在您的剧本旁边的 ansible.cfg 文件中。例如:

ansible.cfg

ansible.cfg

[defaults]
transport = ssh

[ssh_connection]
ssh_args = -o ForwardAgent=yes

I'd say it's better to do that than setting as an env variable, as placing it in a conf file is both more declarative and also will minimise the steps needed for other people you may be working with to going with a project.

我想说这样做比设置为 env 变量要好,因为将它放在 conf 文件中既更具声明性,又可以最大限度地减少与您一起工作的其他人进行项目所需的步骤。

Conf docs: http://docs.ansible.com/intro_configuration.html#the-ansible-configuration-file

Conf 文档:http: //docs.ansible.com/intro_configuration.html#the-ansible-configuration-file

Example config file: https://raw.github.com/ansible/ansible/devel/examples/ansible.cfg

示例配置文件:https: //raw.github.com/ansible/ansible/devel/examples/ansible.cfg

回答by tillda

I want to share the answer that worked for me:

我想分享对我有用的答案:

https://groups.google.com/forum/#!msg/ansible-project/u6o-sWynMjo/69UwJfJPq7cJ- From Ansible Google Group

https://groups.google.com/forum/#!msg/ansible-project/u6o-sWynMjo/69UwJfJPq7cJ- 来自 Ansible Google Group

For ansible, ssh-add to load ssh keys in your host machine first. Then use "ssh" as connection type with forwarding enabled.

Such as:

$ ssh-add  
$ export ANSIBLE_TRANSPORT="ssh"  
$ export  ANSIBLE_SSH_ARGS="-o ForwardAgent=yes"

See manual for ssh-add for running the agent.

对于 ansible,首先使用 ssh-add 在主机中加载 ssh 密钥。然后使用“ssh”作为启用转发的连接类型。

如:

$ ssh-add  
$ export ANSIBLE_TRANSPORT="ssh"  
$ export  ANSIBLE_SSH_ARGS="-o ForwardAgent=yes"

请参阅 ssh-add 手册以运行代理。

The Ansible docs for ssh-argsare http://docs.ansible.com/intro_configuration.html#ssh-args

Ansible 文档ssh-argshttp://docs.ansible.com/intro_configuration.html#ssh-args

回答by locojay

this works for me

这对我有用

- name: ensure known hosts
  shell: touch ~/.ssh/known_hosts
- name: remove github.com from known host
  shell: ssh-keygen -R github.com
  # >> instead of > to keep existing known_hosts file
- name: ensure github.com in known host
  shell: ssh-keyscan -H github.com >> ~/.ssh/known_hosts

回答by Lebnik

Add to ansible.cfg the following parameter:

将以下参数添加到 ansible.cfg:

[defaults]
sudo_flags=-HE

回答by Vini.g.fer

In my case the issue was the repository string. I had a bitbucket private repository set as:

在我的情况下,问题是存储库字符串。我有一个 bitbucket 私有存储库设置为:

git@tsrs...

git@tsrs...

but it should be:

但它应该是:

ssh://git@tsrs...

ssh://git@tsrs...

Notice the subtle absence of the prefix "ssh". The weird part is that if I clone a github repository without the "ssh", it works fine!

请注意前缀“ssh”的微妙缺失。奇怪的是,如果我克隆一个没有“ssh”的 github 存储库,它工作正常!

回答by Nicolas Zozol

I had an error :

我有一个错误:

bitbucket.org has an unknown hostkey. Set accept_hostkey to True or manually add the hostkey prior to running the git module

bitbucket.org 有一个未知的主机密钥。将 accept_hostkey 设置为 True 或在运行 git 模块之前手动添加主机密钥

I had to add a accept_hostkeyparameter to my git module command :

我必须在accept_hostkey我的 git 模块命令中添加一个参数:

playbook :

剧本:

tasks:
    - name: clone
      git: [email protected]:robusta-code/xyz.git
           dest=/app
           accept_hostkey=yes

ansible.cfg

ansible.cfg

[ssh_connection]
ssh_args = -o ForwardAgent=yes