Ansible:如何为用户添加多个 SSH 密钥
ansible_add_ssh_keys.yaml
---
- name: Configure admin SSH keys
hosts: all
become: yes
tasks:
- name: Ensure myuser user exists
user:
name: myuser
shell: /bin/bash
create_home: yes
home: /home/myuser
state: present
- name: Ensure .ssh directory exists
file:
path: /home/myuser/.ssh
state: directory
owner: myuser
group: myuser
mode: '0700'
- name: Add SSH keys to authorized_keys
ansible.posix.authorized_key:
user: myuser
state: present
key: "{{ lookup('file', 'admin_authorized_keys.pub') }}"
exclusive: yes并创建 admin_authorized_keys.pub,其中包含你要添加到用户 authorized_keys 文件的公钥,例如
admin_authorized_keys.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIK32rucsbvMxsTWaSFenYXmKgFU7CaLYNwuDoXpGTig/ user@user-desktop
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHDI/U4vZL3nkm/654cvdzwONKfR+wBeiJQYiJZ4mRD4 user@user-desktopCheck out similar posts by category:
Ansible
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow