Ansible: How to add multiple SSH keys to user
---
- 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
and create admin_authorized_keys.pub
with the public keys you want to add to the user’s authorized_keys
file, e.g.
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIK32rucsbvMxsTWaSFenYXmKgFU7CaLYNwuDoXpGTig/ user@user-desktop
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHDI/U4vZL3nkm/654cvdzwONKfR+wBeiJQYiJZ4mRD4 user@user-desktop
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow