community.crypto.luks_device – Manage encrypted (LUKS) devices

Note

This plugin is part of the community.crypto collection.

To install it use: ansible-galaxy collection install community.crypto.

To use it in a playbook, specify: community.crypto.luks_device.

Synopsis

  • Module manages LUKS on given device. Supports creating, destroying, opening and closing of LUKS container and adding or removing new keys and passphrases.

Requirements

The below requirements are needed on the host that executes this module.

  • cryptsetup

  • wipefs (when state is absent)

  • lsblk

  • blkid (when label or uuid options are used)

Parameters

Parameter Choices/Defaults Comments
cipher
string
added in 1.1.0 of community.crypto
This option allows the user to define the cipher specification string for the LUKS container.
Will only be used on container creation.
For pre-2.6.10 kernels, use aes-plain as they don't understand the new cipher spec strings. To use ESSIV, use aes-cbc-essiv:sha256.
device
string
Device to work with (e.g. /dev/sda1). Needed in most cases. Can be omitted only when state=closed together with name is provided.
force_remove_last_key
boolean
    Choices:
  • no ←
  • yes
If set to yes, allows removing the last key from a container.
BEWARE that when the last key has been removed from a container, the container can no longer be opened!
hash
string
added in 1.1.0 of community.crypto
This option allows the user to specify the hash function used in LUKS key setup scheme and volume key digest.
Will only be used on container creation.
keyfile
path
Used to unlock the container. Either a keyfile or a passphrase is needed for most of the operations. Parameter value is the path to the keyfile with the passphrase.
BEWARE that working with keyfiles in plaintext is dangerous. Make sure that they are protected.
keysize
integer
added in 1.0.0 of community.crypto
Sets the key size only if LUKS container does not exist.
label
string
added in 1.0.0 of community.crypto
This option allow the user to create a LUKS2 format container with label support, respectively to identify the container by label on later usages.
Will only be used on container creation, or when device is not specified.
This cannot be specified if type is set to luks1.
name
string
Sets container name when state=opened. Can be used instead of device when closing the existing container (i.e. when state=closed).
new_keyfile
path
Adds additional key to given container on device. Needs keyfile or passphrase option for authorization. LUKS container supports up to 8 keyslots. Parameter value is the path to the keyfile with the passphrase.
NOTE that adding additional keys is *not idempotent*. A new keyslot will be used even if another keyslot already exists for this keyfile.
BEWARE that working with keyfiles in plaintext is dangerous. Make sure that they are protected.
new_passphrase
string
added in 1.0.0 of community.crypto
Adds additional passphrase to given container on device. Needs keyfile or passphrase option for authorization. LUKS container supports up to 8 keyslots. Parameter value is a string with the new passphrase.
NOTE that adding additional passphrase is *not idempotent*. A new keyslot will be used even if another keyslot already exists for this passphrase.
passphrase
string
added in 1.0.0 of community.crypto
Used to unlock the container. Either a passphrase or a keyfile is needed for most of the operations. Parameter value is a string with the passphrase.
remove_keyfile
path
Removes given key from the container on device. Does not remove the keyfile from filesystem. Parameter value is the path to the keyfile with the passphrase.
NOTE that removing keys is *not idempotent*. Trying to remove a key which no longer exists results in an error.
NOTE that to remove the last key from a LUKS container, the force_remove_last_key option must be set to yes.
BEWARE that working with keyfiles in plaintext is dangerous. Make sure that they are protected.
remove_passphrase
string
added in 1.0.0 of community.crypto
Removes given passphrase from the container on device. Parameter value is a string with the passphrase to remove.
NOTE that removing passphrases is not idempotent. Trying to remove a passphrase which no longer exists results in an error.
NOTE that to remove the last keyslot from a LUKS container, the force_remove_last_key option must be set to yes.
state
string
    Choices:
  • present ←
  • absent
  • opened
  • closed
Desired state of the LUKS container. Based on its value creates, destroys, opens or closes the LUKS container on a given device.
present will create LUKS container unless already present. Requires device and either keyfile or passphrase options to be provided.
absent will remove existing LUKS container if it exists. Requires device or name to be specified.
opened will unlock the LUKS container. If it does not exist it will be created first. Requires device and either keyfile or passphrase to be specified. Use the name option to set the name of the opened container. Otherwise the name will be generated automatically and returned as a part of the result.
closed will lock the LUKS container. However if the container does not exist it will be created. Requires device and either keyfile or passphrase options to be provided. If container does already exist device or name will suffice.
type
string
added in 1.0.0 of community.crypto
    Choices:
  • luks1
  • luks2
This option allow the user explicit define the format of LUKS container that wants to work with. Options are luks1 or luks2
uuid
string
added in 1.0.0 of community.crypto
With this option user can identify the LUKS container by UUID.
Will only be used when device and label are not specified.

Examples

- name: Create LUKS container (remains unchanged if it already exists)
  community.crypto.luks_device:
    device: "/dev/loop0"
    state: "present"
    keyfile: "/vault/keyfile"

- name: Create LUKS container with a passphrase
  community.crypto.luks_device:
    device: "/dev/loop0"
    state: "present"
    passphrase: "foo"

- name: Create LUKS container with specific encryption
  community.crypto.luks_device:
    device: "/dev/loop0"
    state: "present"
    cipher: "aes"
    hash: "sha256"

- name: (Create and) open the LUKS container; name it "mycrypt"
  community.crypto.luks_device:
    device: "/dev/loop0"
    state: "opened"
    name: "mycrypt"
    keyfile: "/vault/keyfile"

- name: Close the existing LUKS container "mycrypt"
  community.crypto.luks_device:
    state: "closed"
    name: "mycrypt"

- name: Make sure LUKS container exists and is closed
  community.crypto.luks_device:
    device: "/dev/loop0"
    state: "closed"
    keyfile: "/vault/keyfile"

- name: Create container if it does not exist and add new key to it
  community.crypto.luks_device:
    device: "/dev/loop0"
    state: "present"
    keyfile: "/vault/keyfile"
    new_keyfile: "/vault/keyfile2"

- name: Add new key to the LUKS container (container has to exist)
  community.crypto.luks_device:
    device: "/dev/loop0"
    keyfile: "/vault/keyfile"
    new_keyfile: "/vault/keyfile2"

- name: Add new passphrase to the LUKS container
  community.crypto.luks_device:
    device: "/dev/loop0"
    keyfile: "/vault/keyfile"
    new_passphrase: "foo"

- name: Remove existing keyfile from the LUKS container
  community.crypto.luks_device:
    device: "/dev/loop0"
    remove_keyfile: "/vault/keyfile2"

- name: Remove existing passphrase from the LUKS container
  community.crypto.luks_device:
    device: "/dev/loop0"
    remove_passphrase: "foo"

- name: Completely remove the LUKS container and its contents
  community.crypto.luks_device:
    device: "/dev/loop0"
    state: "absent"

- name: Create a container with label
  community.crypto.luks_device:
    device: "/dev/loop0"
    state: "present"
    keyfile: "/vault/keyfile"
    label: personalLabelName

- name: Open the LUKS container based on label without device; name it "mycrypt"
  community.crypto.luks_device:
    label: "personalLabelName"
    state: "opened"
    name: "mycrypt"
    keyfile: "/vault/keyfile"

- name: Close container based on UUID
  community.crypto.luks_device:
    uuid: 03ecd578-fad4-4e6c-9348-842e3e8fa340
    state: "closed"
    name: "mycrypt"

- name: Create a container using luks2 format
  community.crypto.luks_device:
    device: "/dev/loop0"
    state: "present"
    keyfile: "/vault/keyfile"
    type: luks2

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key Returned Description
name
string
success
When state=opened returns (generated or given) name of LUKS container. Returns None if no name is supplied.

Sample:
luks-c1da9a58-2fde-4256-9d9f-6ab008b4dd1b


Authors

  • Jan Pokorny (@japokorn)