Skip to content

Commit 27e491c

Browse files
committed
Use authorized_key module and optionally import shh pubkey from GitHub
1 parent 2bc1f49 commit 27e491c

1 file changed

Lines changed: 12 additions & 16 deletions

File tree

roles/unprivileged_user/tasks/main.yml

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,20 @@
2727
create: yes
2828
become: true
2929

30-
- name: "Create {{ unprivileged_user_username }} .ssh"
31-
ansible.builtin.file:
32-
path: "/home/{{ unprivileged_user_username }}/.ssh"
33-
owner: "{{ unprivileged_user_username }}"
34-
group: root
35-
state: directory
36-
mode: 0700
30+
- name: "Add public key to authorized_keys from Host Machine"
31+
ansible.posix.authorized_key:
32+
user: "{{ unprivileged_user_username }}"
33+
state: present
34+
key: "{{ lookup('file', unprivileged_user_import_ssh_pub_key) }}"
35+
when: unprivileged_user_import_ssh_pub_key | default(False)
3736
become: true
3837

39-
- name: "Ensure public key is in authorized_keys"
40-
ansible.builtin.lineinfile:
41-
line: "{{ lookup('file', unprivileged_user_import_ssh_pub_key) }}"
42-
path: "/home/{{ unprivileged_user_username }}/.ssh/authorized_keys"
43-
create: yes
44-
mode: 0600
45-
owner: "{{ unprivileged_user_username }}"
38+
- name: "Add public key to authorized_keys via GitHub"
39+
ansible.posix.authorized_key:
40+
user: "{{ unprivileged_user_username }}"
4641
state: present
47-
when: unprivileged_user_import_ssh_pub_key | default(False)
42+
key: "https://github.com/{{ unprivileged_user_import_ssh_pub_key_github }}.keys"
43+
when: unprivileged_user_import_ssh_pub_key_github | default(False)
4844
become: true
4945

5046
- name: "Check /home/{{ unprivileged_user_username }}/.ssh/authorized_keys"
@@ -53,7 +49,7 @@
5349
register: authorized_keys_file
5450
become: true
5551

56-
- name: "Copy root authorized_keys to unprivileged user"
52+
- name: "Inherit authorized_keys from root user if none imported for {{ unprivileged_user_username }}"
5753
block:
5854
- name: "Check /root/.ssh/authorized_keys"
5955
ansible.builtin.stat:

0 commit comments

Comments
 (0)