gpt4 book ai didi

ssh - 使用 ansible 在远程主机上创建 ssh key 失败

转载 作者:行者123 更新时间:2023-12-04 21:21:49 25 4
gpt4 key购买 nike

我正在使用 Ansible 在远程主机上创建 ssh key 。以下是剧本代码

- name: Test playbook
hosts: all
remote_user: admin
tasks:
- name: Create ssh keys
expect:
command: ssh-keygen -t rsa
echo: yes
timeout: 5
responses:
"file": "" ## Enter file in which to save the key (/home/admin/.ssh/id_rsa)
"Overwrite": "n" ## Overwrite (y/n)?
"passphrase": "" ## Enter passphrase (empty for no passphrase)

但是,它得到以下错误:

fatal: [10.1.1.1]: FAILED! => {"changed": true, "cmd": "ssh-keygen -t rsa", "delta": "0:00:00.301769", "end": "2015-12-30 09:56:29.465815", "failed": true, "invocation": {"module_args": {"chdir": null, "command": "ssh-keygen -t rsa", "creates": null, "echo": true, "removes": null, "responses": {"Overwrite": "n", "file": "", "passphrase": ""}, "timeout": 5}, "module_name": "expect"}, "rc": 1, "start": "2015-12-30 09:56:29.164046", "stdout": "Generating public/private rsa key pair.\r\nEnter file in which to save the key (/home/admin/.ssh/id_rsa): \r\n/home/admin/.ssh/id_rsa already exists.\r\nOverwrite (y/n)? n", "stdout_lines": ["Generating public/private rsa key pair.", "Enter file in which to save the key (/home/admin/.ssh/id_rsa): ", "/home/admin/.ssh/id_rsa already exists.", "Overwrite (y/n)? n"]}

当“Overwrite”被映射到“y”时,这确实工作正常。

最佳答案

This does work fine when "Overwrite" is mapped to "y".

如果是这样,那么听起来您的任务工作正常。 ssh-keygen 只会在文件已经存在的情况下提示覆盖,而你在任务中对“Overwrite”的响应是“n”。如果您告诉 ssh-keygen 不要覆盖该文件,那么它将立即退出并返回非零代码,Ansible 会将其解释为错误。

如果您只想在 key 不存在时执行此任务(为了创建一个新 key 但不覆盖现有 key ),那么您可能需要将以下内容添加到您的任务中:

creates: /home/admin/.ssh/id_rsa

如果指定的文件已经存在,creates 修饰符将阻止任务执行。

关于ssh - 使用 ansible 在远程主机上创建 ssh key 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34535203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com