�
�Udac @` s$ d d l m Z m Z m Z e Z i d d 6d g d 6d d 6Z d Z d Z d
Z d d l
Z
d d l Z d d l Z d d l
m Z d Z d
Z d Z e j d � Z e j d � Z d e f d � � YZ d e f d � � YZ d e f d � � YZ d � Z d � Z e d k r e � n d S( i ( t absolute_importt divisiont print_functions 1.1t metadata_versiont previewt statust communityt supported_bys
---
module: luks_device
short_description: Manage encrypted (LUKS) devices
version_added: "2.8"
description:
- "Module manages L(LUKS,https://en.wikipedia.org/wiki/Linux_Unified_Key_Setup)
on given device. Supports creating, destroying, opening and closing of
LUKS container and adding or removing new keys."
options:
device:
description:
- "Device to work with (e.g. C(/dev/sda1)). Needed in most cases.
Can be omitted only when I(state=closed) together with I(name)
is provided."
type: str
state:
description:
- "Desired state of the LUKS container. Based on its value creates,
destroys, opens or closes the LUKS container on a given device."
- "I(present) will create LUKS container unless already present.
Requires I(device) and I(keyfile) options to be provided."
- "I(absent) will remove existing LUKS container if it exists.
Requires I(device) or I(name) to be specified."
- "I(opened) will unlock the LUKS container. If it does not exist
it will be created first.
Requires I(device) and I(keyfile) to be specified. Use
the I(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."
- "I(closed) will lock the LUKS container. However if the container
does not exist it will be created.
Requires I(device) and I(keyfile) options to be provided. If
container does already exist I(device) or I(name) will suffice."
type: str
default: present
choices: [present, absent, opened, closed]
name:
description:
- "Sets container name when I(state=opened). Can be used
instead of I(device) when closing the existing container
(i.e. when I(state=closed))."
type: str
keyfile:
description:
- "Used to unlock the container and 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."
type: path
new_keyfile:
description:
- "Adds additional key to given container on I(device).
Needs I(keyfile) option for authorization. LUKS container
supports up to 8 keys. Parameter value is the path
to the keyfile with the passphrase."
- "NOTE that adding additional keys is I(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."
type: path
remove_keyfile:
description:
- "Removes given key from the container on I(device). Does not
remove the keyfile from filesystem.
Parameter value is the path to the keyfile with the passphrase."
- "NOTE that removing keys is I(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
I(force_remove_last_key) option must be set to C(yes)."
- "BEWARE that working with keyfiles in plaintext is dangerous.
Make sure that they are protected."
type: path
force_remove_last_key:
description:
- "If set to C(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!"
type: bool
default: no
requirements:
- "cryptsetup"
- "wipefs"
- "lsblk"
author:
"Jan Pokorny (@japokorn)"
s�
- name: create LUKS container (remains unchanged if it already exists)
luks_device:
device: "/dev/loop0"
state: "present"
keyfile: "/vault/keyfile"
- name: (create and) open the LUKS container; name it "mycrypt"
luks_device:
device: "/dev/loop0"
state: "opened"
name: "mycrypt"
keyfile: "/vault/keyfile"
- name: close the existing LUKS container "mycrypt"
luks_device:
state: "closed"
name: "mycrypt"
- name: make sure LUKS container exists and is closed
luks_device:
device: "/dev/loop0"
state: "closed"
keyfile: "/vault/keyfile"
- name: create container if it does not exist and add new key to it
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)
luks_device:
device: "/dev/loop0"
keyfile: "/vault/keyfile"
new_keyfile: "/vault/keyfile2"
- name: remove existing key from the LUKS container
luks_device:
device: "/dev/loop0"
remove_keyfile: "/vault/keyfile2"
- name: completely remove the LUKS container and its contents
luks_device:
device: "/dev/loop0"
state: "absent"
s�
name:
description:
When I(state=opened) returns (generated or given) name
of LUKS container. Returns None if no name is supplied.
returned: success
type: str
sample: "luks-c1da9a58-2fde-4256-9d9f-6ab008b4dd1b"
N( t
AnsibleModulei i s \s*crypt\s+([^\s]*)\s*s \s*device:\s+([^\s]*)\s*t Handlerc B` s# e Z d � Z d � Z d � Z RS( c C` s% | | _ | j j d t � | _ d S( Nt lsblk( t _modulet get_bin_patht Truet
_lsblk_bin( t selft module( ( sF /usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyt __init__� s c C` s | j j | � S( N( R t run_command( R t command( ( sF /usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyt _run_command� s c C` sf | j | j d | d d g � } | t d k rN t d | | t f � � n | t j � } d | S( s� Generate name for luks based on device UUID ('luks-<UUID>').
Raises ValueError when obtaining of UUID fails.
s -ns -ot UUIDi s+ Error while generating LUKS name for %s: %ss luks-%s( R R t RETURN_CODEt
ValueErrort STDERRt STDOUTt strip( R t devicet resultt dev_uuid( ( sF /usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyt generate_luks_name� s !( t __name__t
__module__R R R ( ( ( sF /usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyR � s t CryptHandlerc B` se e Z d � Z d � Z d � Z d � Z d � Z d � Z d � Z d � Z d � Z
e d � Z RS(
c C` s2 t t | � j | � | j j d t � | _ d S( Nt
cryptsetup( t superR! R R R R
t _cryptsetup_bin( R R ( ( sF /usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyR � s c C` s� | j | j | d d g � } | t d k rK t d | | t f � � n t j | t � } y | j d � } Wn t k
r� d } n X| S( s� obtain LUKS container name based on the device where it is located
return None if not found
raise ValueError if lsblk command fails
s -nlos type,namei s* Error while obtaining LUKS name for %s: %si N( R R R R R t LUKS_NAME_REGEXt searchR t groupt AttributeErrort None( R R R t mt name( ( sF /usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyt get_container_name_by_device� s
c C` sU | j | j d | g � } | t d k r/ d St j | t � } | j d � } | S( s� obtain device name based on the LUKS container name
return None if not found
raise ValueError if lsblk command fails
R i i N( R R$ R R) t LUKS_DEVICE_REGEXR&