初始化 $(ssh-agent)
后,我已将 key 添加到 ssh-add
原始 ssh 命令只需 key 且无需密码即可完全正常工作
ssh -i key.pem ubuntu@someip
到目前为止一切顺利,问题是在同一个终端运行时如果我运行 ansible playbook,它会要求输入 key 密码! 并且不允许自动化它
TASK [deploy-all : Copy something to remote host] *******************
Wednesday 30 June 2021 18:04:56 +0200 (0:00:00.609) 0:00:03.063 ********
Enter passphrase for key 'key.pem':
它甚至在同一剧本执行期间多次要求输入密码。 (我有一些任务委托(delegate)给本地主机 delegate_to:“{{ delegate_build_to_host }}”,因此当上下文切换到远程主机时似乎会再次询问密码)
库存文件:
[webserver]
dockerall ansible_host=some.host.ip.xy ansible_user=ubuntu ansible_ssh_private_key_file=key.pem
Playbook 仅使用唯一的主机组网络服务器
---
- hosts: webserver
become: true
gather_facts: false
environment:
CI: "true"
vars:
working_user: root
- { role: deploy, tags: 'deploy' }
部署角色的第一个任务非常简单,我被要求输入密码:
roles/deploy/tasks/main.yaml
# identation is wrong but doesn't matter just copied for example
copy:
src: "{{ role_path }}/files/docker/"
dest: "{{ dc_path }}"
mode: preserve
// more tasks here some delegated to localhost
- name: "create tmp build directory in build host to contain all sort of tmp files that can be deleted after execution"
delegate_to: "localhost"
file:
path: "{{ tmp_build_path }}"
recurse: yes
state: directory
// again after this comment, copy tasks for remote host
关键文件权限:
stat -c "%a %n" key.pem
600 key.pem
最佳答案
设置 ansible_ssh_private_key_file=key.pem
将覆盖您的 ssh-agent 配置,并且 ansible 将询问您每个主机的 ssh 密码(因为它不再查看 ssh-agent 参数并基于只是 ansible 配置)。
从 ansible 文件中删除 ansible_ssh_private_key_file
并在您的服务器的 ~/home/.ssh.config
中添加一个配置 block (示例如下)
Host test
HostName 192.168.5.30
Port 22
User <ssh user to connect>
IdentityFile ~/.ssh/<your ssh private key>
IdentitiesOnly yes
就像这样,您将所有 ssh 配置保存到 ssh-agent
希望这个答案在一年后仍然能帮助一些人
关于ssh - 即使添加到 ssh-add 之后,Ansible 也始终要求提供 ssh key 短语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68198200/