Add active and inactive states to the lvol module (#1974)

* Add `active` and `inactive` states to the lvol module

* Honor the previous state of the changed variable

* Move active/inactive states to active boolean parameter

* Bump version_added to make Travis happy

* Avoid bailing early is size isn't specified

* Add invocation examples

* Move "no size" up for code clarity
This commit is contained in:
Aaron Brady 2016-07-28 16:31:51 +01:00 committed by Brian Coca
parent be367cc2de
commit 3533ae2647

View file

@ -48,8 +48,15 @@ options:
choices: [ "present", "absent" ] choices: [ "present", "absent" ]
default: present default: present
description: description:
- Control if the logical volume exists. If C(present) the C(size) option - Control if the logical volume exists. If C(present) and the
is required. volume does not already exist then the C(size) option is required.
required: false
active:
version_added: "2.2"
choices: [ "yes", "no" ]
default: "yes"
description:
- Whether the volume is activate and visible to the host.
required: false required: false
force: force:
version_added: "1.5" version_added: "1.5"
@ -125,6 +132,12 @@ EXAMPLES = '''
# Create a snapshot volume of the test logical volume. # Create a snapshot volume of the test logical volume.
- lvol: vg=firefly lv=test snapshot=snap1 size=100m - lvol: vg=firefly lv=test snapshot=snap1 size=100m
# Deactivate a logical volume
- lvol: vg=firefly lv=test active=false
# Create a deactivated logical volume
- lvol: vg=firefly lv=test size=512g active=false
''' '''
import re import re
@ -140,7 +153,8 @@ def parse_lvs(data):
parts = line.strip().split(';') parts = line.strip().split(';')
lvs.append({ lvs.append({
'name': parts[0].replace('[','').replace(']',''), 'name': parts[0].replace('[','').replace(']',''),
'size': int(decimal_point.match(parts[1]).group(1)) 'size': int(decimal_point.match(parts[1]).group(1)),
'active': (parts[2][4] == 'a')
}) })
return lvs return lvs
@ -178,6 +192,7 @@ def main():
state=dict(choices=["absent", "present"], default='present'), state=dict(choices=["absent", "present"], default='present'),
force=dict(type='bool', default='no'), force=dict(type='bool', default='no'),
shrink=dict(type='bool', default='yes'), shrink=dict(type='bool', default='yes'),
active=dict(type='bool', default='yes'),
snapshot=dict(type='str', default=None), snapshot=dict(type='str', default=None),
pvs=dict(type='str') pvs=dict(type='str')
), ),
@ -201,6 +216,7 @@ def main():
state = module.params['state'] state = module.params['state']
force = module.boolean(module.params['force']) force = module.boolean(module.params['force'])
shrink = module.boolean(module.params['shrink']) shrink = module.boolean(module.params['shrink'])
active = module.boolean(module.params['active'])
size_opt = 'L' size_opt = 'L'
size_unit = 'm' size_unit = 'm'
snapshot = module.params['snapshot'] snapshot = module.params['snapshot']
@ -270,7 +286,7 @@ def main():
# Get information on logical volume requested # Get information on logical volume requested
lvs_cmd = module.get_bin_path("lvs", required=True) lvs_cmd = module.get_bin_path("lvs", required=True)
rc, current_lvs, err = module.run_command( rc, current_lvs, err = module.run_command(
"%s -a --noheadings --nosuffix -o lv_name,size --units %s --separator ';' %s" % (lvs_cmd, unit, vg)) "%s -a --noheadings --nosuffix -o lv_name,size,lv_attr --units %s --separator ';' %s" % (lvs_cmd, unit, vg))
if rc != 0: if rc != 0:
if state == 'absent': if state == 'absent':
@ -296,8 +312,6 @@ def main():
if state == 'present' and not size: if state == 'present' and not size:
if this_lv is None: if this_lv is None:
module.fail_json(msg="No size given.") module.fail_json(msg="No size given.")
else:
module.exit_json(changed=False, vg=vg, lv=this_lv['name'], size=this_lv['size'])
msg = '' msg = ''
if this_lv is None: if this_lv is None:
@ -325,6 +339,9 @@ def main():
else: else:
module.fail_json(msg="Failed to remove logical volume %s" % (lv), rc=rc, err=err) module.fail_json(msg="Failed to remove logical volume %s" % (lv), rc=rc, err=err)
elif not size:
pass
elif size_opt == 'l': elif size_opt == 'l':
### Resize LV based on % value ### Resize LV based on % value
tool = None tool = None
@ -392,6 +409,22 @@ def main():
else: else:
module.fail_json(msg="Unable to resize %s to %s%s" % (lv, size, size_unit), rc=rc, err=err) module.fail_json(msg="Unable to resize %s to %s%s" % (lv, size, size_unit), rc=rc, err=err)
if this_lv is not None:
if active:
lvchange_cmd = module.get_bin_path("lvchange", required=True)
rc, _, err = module.run_command("%s -ay %s/%s" % (lvchange_cmd, vg, this_lv['name']))
if rc == 0:
module.exit_json(changed=((not this_lv['active']) or changed), vg=vg, lv=this_lv['name'], size=this_lv['size'])
else:
module.fail_json(msg="Failed to activate logical volume %s" % (lv), rc=rc, err=err)
else:
lvchange_cmd = module.get_bin_path("lvchange", required=True)
rc, _, err = module.run_command("%s -an %s/%s" % (lvchange_cmd, vg, this_lv['name']))
if rc == 0:
module.exit_json(changed=(this_lv['active'] or changed), vg=vg, lv=this_lv['name'], size=this_lv['size'])
else:
module.fail_json(msg="Failed to deactivate logical volume %s" % (lv), rc=rc, err=err)
module.exit_json(changed=changed, msg=msg) module.exit_json(changed=changed, msg=msg)
# import module snippets # import module snippets