|
|
@ -72,8 +72,10 @@ EXAMPLES = '''
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
decimal_point = re.compile(r"(\.|,)")
|
|
|
|
decimal_point = re.compile(r"(\.|,)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_lvs(data):
|
|
|
|
def parse_lvs(data):
|
|
|
|
lvs = []
|
|
|
|
lvs = []
|
|
|
|
for line in data.splitlines():
|
|
|
|
for line in data.splitlines():
|
|
|
@ -84,9 +86,10 @@ def parse_lvs(data):
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return lvs
|
|
|
|
return lvs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
argument_spec=dict(
|
|
|
|
vg=dict(required=True),
|
|
|
|
vg=dict(required=True),
|
|
|
|
lv=dict(required=True),
|
|
|
|
lv=dict(required=True),
|
|
|
|
size=dict(),
|
|
|
|
size=dict(),
|
|
|
@ -102,13 +105,10 @@ def main():
|
|
|
|
size_opt = 'L'
|
|
|
|
size_opt = 'L'
|
|
|
|
size_unit = 'm'
|
|
|
|
size_unit = 'm'
|
|
|
|
|
|
|
|
|
|
|
|
if state=='present' and not size:
|
|
|
|
|
|
|
|
module.fail_json(msg="No size given.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if size:
|
|
|
|
if size:
|
|
|
|
# LVCREATE(8) -l --extents option with percentage
|
|
|
|
# LVCREATE(8) -l --extents option with percentage
|
|
|
|
if '%' in size:
|
|
|
|
if '%' in size:
|
|
|
|
size_parts = size.split('%',1)
|
|
|
|
size_parts = size.split('%', 1)
|
|
|
|
size_percent = int(size_parts[0])
|
|
|
|
size_percent = int(size_parts[0])
|
|
|
|
if size_percent > 100:
|
|
|
|
if size_percent > 100:
|
|
|
|
module.fail_json(msg="Size percentage cannot be larger than 100%")
|
|
|
|
module.fail_json(msg="Size percentage cannot be larger than 100%")
|
|
|
@ -141,13 +141,15 @@ def main():
|
|
|
|
unit = 'm'
|
|
|
|
unit = 'm'
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
unit = size_unit
|
|
|
|
unit = size_unit
|
|
|
|
rc,current_lvs,err = module.run_command("lvs --noheadings -o lv_name,size --units %s --separator ';' %s" % (unit, vg))
|
|
|
|
|
|
|
|
|
|
|
|
rc, current_lvs, err = module.run_command(
|
|
|
|
|
|
|
|
"lvs --noheadings -o lv_name,size --units %s --separator ';' %s" % (unit, vg))
|
|
|
|
|
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
if rc != 0:
|
|
|
|
if state == 'absent':
|
|
|
|
if state == 'absent':
|
|
|
|
module.exit_json(changed=False,stdout="Volume group %s does not exist." % vg, stderr=False)
|
|
|
|
module.exit_json(changed=False, stdout="Volume group %s does not exist." % vg, stderr=False)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Volume group %s does not exist."%vg, rc=rc, err=err)
|
|
|
|
module.fail_json(msg="Volume group %s does not exist." % vg, rc=rc, err=err)
|
|
|
|
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
|
|
|
@ -160,6 +162,12 @@ def main():
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
this_lv = None
|
|
|
|
this_lv = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if state == 'present' and not size:
|
|
|
|
|
|
|
|
if this_lv is None:
|
|
|
|
|
|
|
|
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:
|
|
|
|
if state == 'present':
|
|
|
|
if state == 'present':
|
|
|
@ -167,21 +175,21 @@ def main():
|
|
|
|
if module.check_mode:
|
|
|
|
if module.check_mode:
|
|
|
|
changed = True
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
rc,_,err = module.run_command("lvcreate -n %s -%s %s%s %s"%(lv, size_opt, size, size_unit, vg))
|
|
|
|
rc, _, err = module.run_command("lvcreate -n %s -%s %s%s %s" % (lv, size_opt, size, size_unit, vg))
|
|
|
|
if rc == 0:
|
|
|
|
if rc == 0:
|
|
|
|
changed = True
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Creating logical volume '%s' failed"%(lv), rc=rc, err=err)
|
|
|
|
module.fail_json(msg="Creating logical volume '%s' failed" % (lv), rc=rc, err=err)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
if state == 'absent':
|
|
|
|
if state == 'absent':
|
|
|
|
### remove LV
|
|
|
|
### remove LV
|
|
|
|
if module.check_mode:
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
rc,_,err = module.run_command("lvremove --force %s/%s"%(vg,this_lv['name']))
|
|
|
|
rc, _, err = module.run_command("lvremove --force %s/%s" % (vg, this_lv['name']))
|
|
|
|
if rc == 0:
|
|
|
|
if rc == 0:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
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 size_opt == 'l':
|
|
|
|
elif size_opt == 'l':
|
|
|
|
module.exit_json(changed=False, msg="Resizing extents with percentage not supported.")
|
|
|
|
module.exit_json(changed=False, msg="Resizing extents with percentage not supported.")
|
|
|
@ -197,14 +205,17 @@ def main():
|
|
|
|
if module.check_mode:
|
|
|
|
if module.check_mode:
|
|
|
|
changed = True
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
rc,_,err = module.run_command("%s -%s %s%s %s/%s"%(tool, size_opt, size, size_unit, vg, this_lv['name']))
|
|
|
|
rc, _, err = module.run_command("%s -%s %s%s %s/%s" % (tool, size_opt, size, size_unit, vg, this_lv['name']))
|
|
|
|
if rc == 0:
|
|
|
|
if rc == 0:
|
|
|
|
changed = True
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
elif "matches existing size" in err:
|
|
|
|
|
|
|
|
module.exit_json(changed=False, vg=vg, lv=this_lv['name'], size=this_lv['size'])
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
module.exit_json(changed=changed,msg=msg)
|
|
|
|
module.exit_json(changed=changed, msg=msg)
|
|
|
|
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
|
|
|
|
|
|
|
|
main()
|
|
|
|
main()
|
|
|
|