vm/install: Bake authorized_keys in the initrd
authornicoo <nicoo@realraum.at>
Wed, 13 Jun 2018 17:34:36 +0000 (19:34 +0200)
committernicoo <nicoo@realraum.at>
Sun, 17 Jun 2018 10:05:29 +0000 (12:05 +0200)
This is less error-prone than cramming the SSH keys into the preseed.
Also, support setting VM-specific SSH keys.

ansible/roles/vm/install/tasks/main.yml
ansible/roles/vm/install/templates/preseed_debian-stretch.cfg.j2

index 39e625f..aaa881e 100644 (file)
         src: "preseed_{{ vmdistro }}-{{ vmdistcodename }}.cfg.j2"
         dest: "{{ tmpdir }}/preseed.cfg"
 
+    - name: generate authorized_keys file
+      authorized_key:
+        user: root
+        path: "{{ tmpdir }}/authorized_keys"
+        key: "{{ hostvars[vmname].ssh_keys | default(noc_ssh_keys) | join('\n') }}"
+
     - name: Inject files into initramfs
       shell: cpio -H newc -o | gzip -9 >> {{ initramfs }}
       args:
         chdir: "{{ tmpdir }}"
         stdin: |
           preseed.cfg
+          authorized_keys
 
   always:
     - name: Delete temporary directory
index 6d48fd2..6cf4b9b 100644 (file)
@@ -102,5 +102,7 @@ d-i finish-install/reboot_in_progress note
 d-i preseed/late_command string \
     lvremove -f {{ vmname }}/dummy; \
     in-target bash -c "apt-get update -q && apt-get full-upgrade -y -q"; \
-    in-target bash -c "passwd -d root; passwd -l root; umask 077; mkdir -p /root/.ssh/; echo -e '{{ noc_ssh_keys | join('\\n') }}' > /root/.ssh/authorized_keys"; \
+    in-target bash -c "passwd -d root && passwd -l root"; \
+    mkdir -p -m 0700 /target/root/.ssh; \
+    cp /authorized_keys /target/root/.ssh/; \
     in-target bash -c "sed -e 's/^\(\s*#*\s*Port.*\)/Port 22000/' -i /etc/ssh/sshd_config"