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

pull/2357/head
Alexander Bulimov 12 years ago
parent c8c0fe945b
commit 902d6347c7

@ -32,9 +32,9 @@ options:
description:
- The name of the volume group.
required: true
dev:
pvs:
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
pesize:
description:
@ -51,13 +51,13 @@ options:
choices: [ "yes", "no" ]
default: "no"
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
examples:
- 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.
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.
code: lvg vg=vg.services state=absent
notes:
@ -89,8 +89,8 @@ def main():
module = AnsibleModule(
argument_spec = dict(
vg=dict(required=True),
dev=dict(),
pesize=dict(),
pvs=dict(type='list'),
pesize=dict(type='int', default=4),
state=dict(choices=["absent", "present"], default='present'),
force=dict(type='bool', default='no'),
),
@ -100,22 +100,19 @@ def main():
vg = module.params['vg']
state = module.params['state']
force = module.boolean(module.params['force'])
if module.params['pesize']:
pesize = int(module.params['pesize'])
else:
pesize = 4
pesize = module.params['pesize']
if module.params['dev']:
dev = module.params['dev'].replace(',',' ')
dev_list = module.params['dev'].split(',')
if module.params['pvs']:
dev_string = ' '.join(module.params['pvs'])
dev_list = module.params['pvs']
elif state == 'present':
module.fail_json(msg="No dev given.")
module.fail_json(msg="No physical volumes given.")
if state=='present':
### check given devices
for test_dev in dev_list:
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
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
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:
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 ';'")
@ -157,7 +154,7 @@ def main():
changed = True
else:
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:
changed = True
else:
@ -178,27 +175,16 @@ def main():
module.fail_json(msg="Refuse to remove non-empty volume group %s without force=yes"%(vg))
### resize VG
action = None
current_devs = map(lambda x: x['name'], filter(lambda pv: pv['vg_name'] == vg, pvs))
current_devs = [ pv['name'] for pv in pvs if pv['vg_name'] == vg ]
devs_to_remove = list(set(current_devs) - set(dev_list))
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:
changed = True
else:
if action == 'extend' or action == 'modify':
tool = 'vgextend'
if devs_to_add:
devs_to_add_string = ' '.join(devs_to_add)
### create PV
for current_dev in devs_to_add:
rc,_,err = module.run_command("pvcreate %s"%current_dev)
@ -206,19 +192,17 @@ def main():
changed = True
else:
module.fail_json(msg="Creating physical volume '%s' failed"%current_dev, rc=rc, err=err)
else:
tool = 'vgreduce --force'
### add PV to our VG
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
rc,_,err = module.run_command("%s %s %s"%(tool, vg, devs_string))
if rc == 0:
changed = True
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))
### remove some PV from our VG
if devs_to_remove:
devs_to_remove_string = ' '.join(devs_to_remove)
rc,_,err = module.run_command("vgreduce --force %s %s"%(vg, devs_to_remove_string))
if rc == 0:
changed = True
else:

Loading…
Cancel
Save