The Ansible Role For LUKS VM template

The Ansible Role For LUKS VM template

This is the role that does the actions I described previously. I decided to break it out to its own doc to make it easier for someone to skip if they don't care about the ansible part.

tasks/main.yml

First we will install programs we will need

- name: Update apt, and install basic tools needed for clevis
  apt:
    name: "{{ packages }}"
    state: present
  vars:
    packages:
      - clevis
      - clevis-initramfs
      - clevis-luks
      - clevis-tpm2
      - clevis-systemd

Then figure out the device that / is mounted from. So root_dev can be used as a variable later to refer to this device. This will look like /dev/mapper/root-crypt

- name: mount device name
  ansible.builtin.set_fact:
    root_dev: "{{ ansible_mounts | json_query('[?mount == `/`].device') | json_query('[0]') | string }}"

Set crypt_vol_name variable to just the volume name part (the root-crypt part of /dev/mapper/root-crypt).

If the / is not mounted from something in /dev/mapper, then you are probably running this role against a machine that doesn't have the root filesystem encrypted. If that's the case, we can just skip the rest of this. I'm sure there's a better way to exit early, but I haven't found it yet.

If the root is mounted from a device in /dev/mapper, go on to do the rest of this. Otherwise just skip to the end of this file.

tasks/clevis_setup.yml

Find out what device contains the LUKS volume (this should look like /dev/sda3).

Verify that the fallback password is set. This should be a variable set in the inventory. It should be encrypted with ansible vault. If the variable is not set, or wasn't read because we didn't include the --ask-vault-pass switch, end running the playbook.

Do the re-encrypt so this VM doesn't use the same key as the template or any other VM.

I put my tang servers on the IP ending in 254 in each of my subnets. This sets that IP as the variable tang_ip

Add tang server as a clevis pin that can unlock LUKS.

Add the fallback password to LUKS.

Remove the old initial_password file as a password from LUKS, and delete the file.

Change crypttab to not expect to find the password in a file anymore.

Change the initrd config to not include the initial-password file anymore, and then rebuild the initrd.

Reboot so if there were any problems we find out before wasting any more time on the setup.

And that's it. With this every server can have encrypted disks without it being a big hassle. .

Last updated