Skip to content

Instantly share code, notes, and snippets.

@MarshalW
Created November 22, 2020 10:12
Show Gist options
  • Save MarshalW/49a54b6287f9d908e6833b16f5b99388 to your computer and use it in GitHub Desktop.
Save MarshalW/49a54b6287f9d908e6833b16f5b99388 to your computer and use it in GitHub Desktop.
ansible-playbook将a服务器ssh key设置到b服务器

ansible-playbook将a服务器ssh key设置到b服务器

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment