now using 'type=' for module parameters, replaced lambda with list comprehensions, simplyfied extend/reduce part of module, renamed dev parameter to pvs

This commit is contained in:
Alexander Bulimov 2013-03-14 13:32:16 +04:00
parent c8c0fe945b
commit 902d6347c7

View file

@ -32,9 +32,9 @@ options:
description: description:
- The name of the volume group. - The name of the volume group.
required: true required: true
dev: pvs:
description: description:
- List of comma-separated devices to use in this volume group. - List of comma-separated devices to use as physical devices in this volume group.
required: true required: true
pesize: pesize:
description: description:
@ -51,13 +51,13 @@ options:
choices: [ "yes", "no" ] choices: [ "yes", "no" ]
default: "no" default: "no"
description: description:
- If yes, allows to remove volume group with logical volumes. - If yes, allows to remove or reduce volume group with logical volumes.
required: false required: false
examples: examples:
- description: Create a volume group on top of /dev/sda1 with physical extent size = 32MB. - description: Create a volume group on top of /dev/sda1 with physical extent size = 32MB.
code: lvg vg=vg.services dev=/dev/sda1 pesize=32 code: lvg vg=vg.services pvs=/dev/sda1 pesize=32
- description: Create a volume group on top of /dev/sdb1 and /dev/sdc5. - description: Create a volume group on top of /dev/sdb1 and /dev/sdc5.
code: lvg vg=vg.services dev=/dev/sdb1,/dev/sdc5 code: lvg vg=vg.services pvs=/dev/sdb1,/dev/sdc5
- description: Remove a volume group with name vg.services. - description: Remove a volume group with name vg.services.
code: lvg vg=vg.services state=absent code: lvg vg=vg.services state=absent
notes: notes:
@ -89,8 +89,8 @@ def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
vg=dict(required=True), vg=dict(required=True),
dev=dict(), pvs=dict(type='list'),
pesize=dict(), pesize=dict(type='int', default=4),
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'),
), ),
@ -100,22 +100,19 @@ def main():
vg = module.params['vg'] vg = module.params['vg']
state = module.params['state'] state = module.params['state']
force = module.boolean(module.params['force']) force = module.boolean(module.params['force'])
if module.params['pesize']: pesize = module.params['pesize']
pesize = int(module.params['pesize'])
else:
pesize = 4
if module.params['dev']: if module.params['pvs']:
dev = module.params['dev'].replace(',',' ') dev_string = ' '.join(module.params['pvs'])
dev_list = module.params['dev'].split(',') dev_list = module.params['pvs']
elif state == 'present': elif state == 'present':
module.fail_json(msg="No dev given.") module.fail_json(msg="No physical volumes given.")
if state=='present': if state=='present':
### check given devices ### check given devices
for test_dev in dev_list: for test_dev in dev_list:
if not os.path.exists(test_dev): if not os.path.exists(test_dev):
module.fail_json(msg="No dev %s found."%test_dev) module.fail_json(msg="Device %s not found."%test_dev)
### get pv list ### get pv list
rc,current_pvs,err = module.run_command("pvs --noheadings -o pv_name,vg_name --separator ';'") rc,current_pvs,err = module.run_command("pvs --noheadings -o pv_name,vg_name --separator ';'")
@ -124,9 +121,9 @@ def main():
### check pv for devices ### check pv for devices
pvs = parse_pvs(current_pvs) pvs = parse_pvs(current_pvs)
used_pvs = filter(lambda pv: pv['name'] in dev_list and pv['vg_name'] and pv['vg_name'] != vg, pvs) used_pvs = [ pv for pv in pvs if pv['name'] in dev_list and pv['vg_name'] and pv['vg_name'] != vg ]
if used_pvs: if used_pvs:
module.fail_json(msg="dev %s is already in %s volume group."%(used_pvs[0]['name'],used_pvs[0]['vg_name'])) module.fail_json(msg="Device %s is already in %s volume group."%(used_pvs[0]['name'],used_pvs[0]['vg_name']))
rc,current_vgs,err = module.run_command("vgs --noheadings -o vg_name,pv_count,lv_count --separator ';'") rc,current_vgs,err = module.run_command("vgs --noheadings -o vg_name,pv_count,lv_count --separator ';'")
@ -157,7 +154,7 @@ def main():
changed = True changed = True
else: else:
module.fail_json(msg="Creating physical volume '%s' failed"%current_dev, rc=rc, err=err) module.fail_json(msg="Creating physical volume '%s' failed"%current_dev, rc=rc, err=err)
rc,_,err = module.run_command("vgcreate -s %s %s %s"%(pesize, vg, dev)) rc,_,err = module.run_command("vgcreate -s %s %s %s"%(pesize, vg, dev_string))
if rc == 0: if rc == 0:
changed = True changed = True
else: else:
@ -178,27 +175,16 @@ def main():
module.fail_json(msg="Refuse to remove non-empty volume group %s without force=yes"%(vg)) module.fail_json(msg="Refuse to remove non-empty volume group %s without force=yes"%(vg))
### resize VG ### resize VG
action = None current_devs = [ pv['name'] for pv in pvs if pv['vg_name'] == vg ]
current_devs = map(lambda x: x['name'], filter(lambda pv: pv['vg_name'] == vg, pvs))
devs_to_remove = list(set(current_devs) - set(dev_list)) devs_to_remove = list(set(current_devs) - set(dev_list))
devs_to_add = list(set(dev_list) - set(current_devs)) devs_to_add = list(set(dev_list) - set(current_devs))
if devs_to_remove and devs_to_add:
devs_string = ' '.join([`dev` for dev in devs_to_add])
devs_to_remove_string = ' '.join([`dev` for dev in devs_to_remove])
action = 'modify'
elif devs_to_remove:
devs_string = ' '.join([`dev` for dev in devs_to_remove])
action = 'reduce'
elif devs_to_add:
devs_string = ' '.join([`dev` for dev in devs_to_add])
action = 'extend'
if action: if devs_to_add or devs_to_remove:
if module.check_mode: if module.check_mode:
changed = True changed = True
else: else:
if action == 'extend' or action == 'modify': if devs_to_add:
tool = 'vgextend' devs_to_add_string = ' '.join(devs_to_add)
### create PV ### create PV
for current_dev in devs_to_add: for current_dev in devs_to_add:
rc,_,err = module.run_command("pvcreate %s"%current_dev) rc,_,err = module.run_command("pvcreate %s"%current_dev)
@ -206,19 +192,17 @@ def main():
changed = True changed = True
else: else:
module.fail_json(msg="Creating physical volume '%s' failed"%current_dev, rc=rc, err=err) module.fail_json(msg="Creating physical volume '%s' failed"%current_dev, rc=rc, err=err)
else: ### add PV to our VG
tool = 'vgreduce --force' rc,_,err = module.run_command("vgextend %s %s"%(vg, devs_to_add_string))
if rc == 0:
changed = True
else:
module.fail_json(msg="Unable to extend %s by %s."%(vg, devs_to_add_string),rc=rc,err=err)
### first we add or remove PV ### remove some PV from our VG
rc,_,err = module.run_command("%s %s %s"%(tool, vg, devs_string)) if devs_to_remove:
if rc == 0: devs_to_remove_string = ' '.join(devs_to_remove)
changed = True rc,_,err = module.run_command("vgreduce --force %s %s"%(vg, devs_to_remove_string))
else:
module.fail_json(msg="Unable to %s %s by %s."%(action, vg, devs_string),rc=rc,err=err)
### then if we need - remove some PV
if action == 'modify':
tool = 'vgreduce --force'
rc,_,err = module.run_command("%s %s %s"%(tool, vg, devs_to_remove_string))
if rc == 0: if rc == 0:
changed = True changed = True
else: else: