From 0b453abfa4b8ec4b9948b6a0c48bde6ff7b865c2 Mon Sep 17 00:00:00 2001 From: Tuan Anh Hoang-Vu Date: Mon, 4 Mar 2019 10:29:24 -0500 Subject: [PATCH] [backport 2.7] Fixed lvol ValueError with float size. (#52836) * Fixed lvol ValueError with float size. (cherry picked from commit 85bd54dfa7dfc710d5a4eafecd077338e47382ab) * Fixed lvol ValueError with float size. (cherry picked from commit ecdd835b6c98e36d51c2784358d3a7c79fc7175f) * Initialized locale using system default. Changed size validation from float() to locale.atof(). (cherry picked from commit d187b9592932d99ea3bdcecba8f038c45d14dac3) * Added changelog fragment. (cherry picked from commit d1f6b1220c4412dd18deb6f0574cf82caf949029) * Used C locale instead of relying on system locale. (cherry picked from commit 90b3d968690bbd7bfdc670f91f3cfd7b39dc6534) --- ...1-fix-lvol-valueerror-with-float-size.yaml | 2 ++ lib/ansible/modules/system/lvol.py | 27 ++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/36811-fix-lvol-valueerror-with-float-size.yaml diff --git a/changelogs/fragments/36811-fix-lvol-valueerror-with-float-size.yaml b/changelogs/fragments/36811-fix-lvol-valueerror-with-float-size.yaml new file mode 100644 index 00000000000..f784555ee7f --- /dev/null +++ b/changelogs/fragments/36811-fix-lvol-valueerror-with-float-size.yaml @@ -0,0 +1,2 @@ +bugfixes: + - lvol - fixed ValueError when using float size (https://github.com/ansible/ansible/issues/32886, https://github.com/ansible/ansible/issues/29429) diff --git a/lib/ansible/modules/system/lvol.py b/lib/ansible/modules/system/lvol.py index d123e0fe6ca..e16ad54783d 100644 --- a/lib/ansible/modules/system/lvol.py +++ b/lib/ansible/modules/system/lvol.py @@ -211,7 +211,14 @@ import re from ansible.module_utils.basic import AnsibleModule -decimal_point = re.compile(r"(\d+)") + +LVOL_ENV_VARS = dict( + # make sure we use the C locale when running lvol-related commands + LANG='C', + LC_ALL='C', + LC_MESSAGES='C', + LC_CTYPE='C', +) def mkversion(major, minor, patch): @@ -224,7 +231,7 @@ def parse_lvs(data): parts = line.strip().split(';') lvs.append({ 'name': parts[0].replace('[', '').replace(']', ''), - 'size': int(decimal_point.match(parts[1]).group(1)), + 'size': float(parts[1]), 'active': (parts[2][4] == 'a'), 'thinpool': (parts[2][0] == 't'), 'thinvol': (parts[2][0] == 'V'), @@ -238,9 +245,9 @@ def parse_vgs(data): parts = line.strip().split(';') vgs.append({ 'name': parts[0], - 'size': int(decimal_point.match(parts[1]).group(1)), - 'free': int(decimal_point.match(parts[2]).group(1)), - 'ext_size': int(decimal_point.match(parts[3]).group(1)) + 'size': float(parts[1]), + 'free': float(parts[2]), + 'ext_size': float(parts[3]) }) return vgs @@ -278,6 +285,8 @@ def main(): ), ) + module.run_command_environ_update = LVOL_ENV_VARS + # Determine if the "--yes" option should be used version_found = get_lvm_version(module) if version_found is None: @@ -354,7 +363,7 @@ def main(): # Get information on volume group requested vgs_cmd = module.get_bin_path("vgs", required=True) rc, current_vgs, err = module.run_command( - "%s --noheadings -o vg_name,size,free,vg_extent_size --units %s --separator ';' %s" % (vgs_cmd, unit, vg)) + "%s --noheadings --nosuffix -o vg_name,size,free,vg_extent_size --units %s --separator ';' %s" % (vgs_cmd, unit, vg)) if rc != 0: if state == 'absent': @@ -507,10 +516,10 @@ def main(): else: # resize LV based on absolute values tool = None - if int(size) > this_lv['size']: + if float(size) > this_lv['size']: tool = module.get_bin_path("lvextend", required=True) - elif shrink and int(size) < this_lv['size']: - if int(size) == 0: + elif shrink and float(size) < this_lv['size']: + if float(size) == 0: module.fail_json(msg="Sorry, no shrinking of %s to 0 permitted." % (this_lv['name'])) if not force: module.fail_json(msg="Sorry, no shrinking of %s without force=yes." % (this_lv['name']))