playbook.yml:
---
- hosts: a
gather_facts: no
tasks:
- name: get ssh key
slurp:
src: .ssh/id_rsa.pub
register: slurpfile
- hosts: b
gather_facts: no
tasks:
- name: "Set authorized key took from file"
authorized_key:
user: user-name
state: present
key: "{{ hostvars['a']['slurpfile'].content | b64decode }}"
另外,在a服务器,~/.ssh/config:
Host *
IdentitiesOnly=yes
AddKeysToAgent yes
IgnoreUnknown UseKeychain
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
host b
HostName 1.2.3.4
Port 1234
User user-name