diff options
author | Romain Gonçalves <me@rgoncalves.se> | 2021-07-29 17:00:20 +0200 |
---|---|---|
committer | Romain Gonçalves <me@rgoncalves.se> | 2021-07-29 17:00:20 +0200 |
commit | b232b894c3cbe087c8d504e91637dcf62199eed1 (patch) | |
tree | ea72a7d1a7f63c6425ee46785e1cec924351b2c3 /roles/sshd | |
parent | 22126073344e60c405d086df37b64da0c6d3c086 (diff) | |
download | infrastructure-b232b894c3cbe087c8d504e91637dcf62199eed1.tar.gz |
Bump to third revision of homelab
Diffstat (limited to 'roles/sshd')
-rw-r--r-- | roles/sshd/tasks/main.yml | 70 | ||||
-rw-r--r-- | roles/sshd/tasks/sync_keys.yml | 68 |
2 files changed, 70 insertions, 68 deletions
diff --git a/roles/sshd/tasks/main.yml b/roles/sshd/tasks/main.yml index 61bbcb7..b34d783 100644 --- a/roles/sshd/tasks/main.yml +++ b/roles/sshd/tasks/main.yml @@ -12,74 +12,8 @@ group: "0" mode: "0644" -- name: retrieve all existing users - shell: cut -d ":" -f 1 /etc/passwd - register: sshd_users - changed_when: false - -- name: bind retrieved users output lines to list - set_fact: - sshd_users: "{{ sshd_users.stdout_lines }}" - -- name: get ssh keys for all user - find: - paths: "{{ inventory_dir }}/files/pubkeys" - pattern: "*.pub" - recurse: true - file_type: link - register: keys - delegate_to: localhost - -- name: show pubkeys - debug: - msg: | - {% for key in keys.files %} - {{ key.path }} - {% endfor %} - run_once: true - delegate_to: localhost - -- name: synchronize ssh keys - authorized_key: - user: "{{ item.path | dirname | basename }}" - state: present - key: "{{ lookup('file', item.path) }}" - when: item.path | dirname | basename in sshd_users - loop: "{{ keys.files }}" - loop_control: - label: "{{ item.path }}" - -- name: get users homedir - shell: echo $(getent passwd "{{ item.path | dirname | basename }}" | cut -d ":" -f 6) "{{ item.path | dirname | basename }}" - register: sshd_homedirs - when: item.path | dirname | basename in sshd_users - loop: "{{ keys.files }}" - changed_when: false - loop_control: - label: "{{ item.path | dirname | basename }}" - -- name: clean users homedir result - set_fact: - sshd_homedirs: "[{% for dir in sshd_homedirs.results if dir.stdout is defined %}\"{{ dir.stdout }}\", {% endfor %}]" - -- name: make users homedir unique - set_fact: - sshd_homedirs: "{{ sshd_homedirs | unique }}" - -- name: show sshd homedirs for users - debug: - var: sshd_homedirs - -- name: chown ssh file to correct user - file: - path: "{{ item.split(' ')[0] }}/.ssh/authorized_keys" - owner: "{{ item.split(' ')[1] }}" - mode: "0600" - ignore_errors: true - when: item.split(" ")[1] in sshd_users - loop: "{{ sshd_homedirs }}" - loop_control: - label: "{{ item }}" +- name: synchronize keys + include_tasks: sync_keys.yml - name: restart sshd service: diff --git a/roles/sshd/tasks/sync_keys.yml b/roles/sshd/tasks/sync_keys.yml new file mode 100644 index 0000000..73aa741 --- /dev/null +++ b/roles/sshd/tasks/sync_keys.yml @@ -0,0 +1,68 @@ +- name: retrieve all existing users + shell: cut -d ":" -f 1 /etc/passwd + register: sshd_users + changed_when: false + +- name: bind retrieved users output lines to list + set_fact: + sshd_users: "{{ sshd_users.stdout_lines }}" + +- name: get ssh keys for all user + find: + paths: "{{ inventory_dir }}/files/pubkeys" + pattern: "*.pub" + recurse: true + file_type: link + register: keys + delegate_to: localhost + +- name: show pubkeys + debug: + msg: | + {% for key in keys.files %} + {{ key.path }} + {% endfor %} + run_once: true + delegate_to: localhost + +- name: synchronize ssh keys + authorized_key: + user: "{{ item.path | dirname | basename }}" + state: present + key: "{{ lookup('file', item.path) }}" + when: item.path | dirname | basename in sshd_users + loop: "{{ keys.files }}" + loop_control: + label: "{{ item.path }}" + +- name: get users homedir + shell: echo $(getent passwd "{{ item.path | dirname | basename }}" | cut -d ":" -f 6) "{{ item.path | dirname | basename }}" + register: sshd_homedirs + when: item.path | dirname | basename in sshd_users + loop: "{{ keys.files }}" + changed_when: false + loop_control: + label: "{{ item.path | dirname | basename }}" + +- name: clean users homedir result + set_fact: + sshd_homedirs: "[{% for dir in sshd_homedirs.results if dir.stdout is defined %}\"{{ dir.stdout }}\", {% endfor %}]" + +- name: make users homedir unique + set_fact: + sshd_homedirs: "{{ sshd_homedirs | unique }}" + +- name: show sshd homedirs for users + debug: + var: sshd_homedirs + +- name: chown ssh file to correct user + file: + path: "{{ item.split(' ')[0] }}/.ssh/authorized_keys" + owner: "{{ item.split(' ')[1] }}" + mode: "0600" + ignore_errors: true + when: item.split(" ")[1] in sshd_users + loop: "{{ sshd_homedirs }}" + loop_control: + label: "{{ item }}" |