From da1dd21a9e134e0e1b7c06840f434ce0e7decda3 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 2 Mar 2022 14:05:07 +0100 Subject: [PATCH] Fix parsing of lsblk output. (#410) (#413) (cherry picked from commit 0d4b3ed9916469b8f21f2fc302233201eba8eb50) Co-authored-by: Felix Fontein --- .../fragments/410-luks_device-lsblk-parsing.yml | 2 ++ plugins/modules/luks_device.py | 14 ++++++-------- 2 files changed, 8 insertions(+), 8 deletions(-) create mode 100644 changelogs/fragments/410-luks_device-lsblk-parsing.yml diff --git a/changelogs/fragments/410-luks_device-lsblk-parsing.yml b/changelogs/fragments/410-luks_device-lsblk-parsing.yml new file mode 100644 index 00000000..8c6b2a3f --- /dev/null +++ b/changelogs/fragments/410-luks_device-lsblk-parsing.yml @@ -0,0 +1,2 @@ +bugfixes: + - "luks_device - fix parsing of ``lsblk`` output when device name ends with ``crypt`` (https://github.com/ansible-collections/community.crypto/issues/409, https://github.com/ansible-collections/community.crypto/pull/410)." diff --git a/plugins/modules/luks_device.py b/plugins/modules/luks_device.py index e3a744c2..fc49d271 100644 --- a/plugins/modules/luks_device.py +++ b/plugins/modules/luks_device.py @@ -350,7 +350,7 @@ STDERR = 2 # used to get out of lsblk output in format 'crypt ' # regex takes care of any possible blank characters -LUKS_NAME_REGEX = re.compile(r'\s*crypt\s+([^\s]*)\s*') +LUKS_NAME_REGEX = re.compile(r'^crypt\s+([^\s]*)\s*$') # used to get out of lsblk output # in format 'device: ' LUKS_DEVICE_REGEX = re.compile(r'\s*device:\s+([^\s]*)\s*') @@ -446,13 +446,11 @@ class CryptHandler(Handler): raise ValueError('Error while obtaining LUKS name for %s: %s' % (device, result[STDERR])) - m = LUKS_NAME_REGEX.search(result[STDOUT]) - - try: - name = m.group(1) - except AttributeError: - name = None - return name + for line in result[STDOUT].splitlines(False): + m = LUKS_NAME_REGEX.match(line) + if m: + return m.group(1) + return None def get_container_device_by_name(self, name): ''' obtain device name based on the LUKS container name