2013-02-27 11:24:56 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-03-11 09:20:01 +01:00
|
|
|
# (c) 2013, Jeroen Hoekx <jeroen.hoekx@dsquare.be>, Alexander Bulimov <lazywolf0@gmail.com>
|
2013-02-27 11:24:56 +01:00
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
2015-05-14 07:08:41 +02:00
|
|
|
author:
|
|
|
|
- "Jeroen Hoekx (@jhoekx)" <jeroen.hoekx@dsquare.be>
|
|
|
|
- "Alexander Bulimov (@abulimov)" <lazywolf0@gmail.com>
|
2013-02-27 11:24:56 +01:00
|
|
|
module: lvol
|
|
|
|
short_description: Configure LVM logical volumes
|
|
|
|
description:
|
|
|
|
- This module creates, removes or resizes logical volumes.
|
|
|
|
version_added: "1.1"
|
|
|
|
options:
|
|
|
|
vg:
|
|
|
|
description:
|
|
|
|
- The volume group this logical volume is part of.
|
|
|
|
required: true
|
|
|
|
lv:
|
|
|
|
description:
|
|
|
|
- The name of the logical volume.
|
|
|
|
required: true
|
|
|
|
size:
|
|
|
|
description:
|
2013-06-24 22:37:31 +02:00
|
|
|
- The size of the logical volume, according to lvcreate(8) --size, by
|
|
|
|
default in megabytes or optionally with one of [bBsSkKmMgGtTpPeE] units; or
|
|
|
|
according to lvcreate(8) --extents as a percentage of [VG|PVS|FREE];
|
|
|
|
resizing is not supported with percentages.
|
2013-02-27 11:24:56 +01:00
|
|
|
state:
|
|
|
|
choices: [ "present", "absent" ]
|
|
|
|
default: present
|
|
|
|
description:
|
|
|
|
- Control if the logical volume exists.
|
|
|
|
required: false
|
2014-01-26 18:07:48 +01:00
|
|
|
force:
|
|
|
|
version_added: "1.5"
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "no"
|
|
|
|
description:
|
|
|
|
- Shrink or remove operations of volumes requires this switch. Ensures that
|
|
|
|
that filesystems get never corrupted/destroyed by mistake.
|
|
|
|
required: false
|
2013-02-27 11:24:56 +01:00
|
|
|
notes:
|
|
|
|
- Filesystems on top of the volume are not resized.
|
|
|
|
'''
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Create a logical volume of 512m.
|
|
|
|
- lvol: vg=firefly lv=test size=512
|
|
|
|
|
2013-06-24 22:37:31 +02:00
|
|
|
# Create a logical volume of 512g.
|
|
|
|
- lvol: vg=firefly lv=test size=512g
|
|
|
|
|
|
|
|
# Create a logical volume the size of all remaining space in the volume group
|
|
|
|
- lvol: vg=firefly lv=test size=100%FREE
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
# Extend the logical volume to 1024m.
|
|
|
|
- lvol: vg=firefly lv=test size=1024
|
|
|
|
|
|
|
|
# Reduce the logical volume to 512m
|
2014-01-26 18:07:48 +01:00
|
|
|
- lvol: vg=firefly lv=test size=512 force=yes
|
2013-06-14 11:53:43 +02:00
|
|
|
|
|
|
|
# Remove the logical volume.
|
2014-01-26 18:07:48 +01:00
|
|
|
- lvol: vg=firefly lv=test state=absent force=yes
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
2013-09-24 15:06:18 +02:00
|
|
|
import re
|
2014-02-24 23:56:08 +01:00
|
|
|
|
2013-09-24 16:19:07 +02:00
|
|
|
decimal_point = re.compile(r"(\.|,)")
|
2013-09-24 15:06:18 +02:00
|
|
|
|
2014-02-24 23:56:08 +01:00
|
|
|
|
2013-02-27 11:24:56 +01:00
|
|
|
def parse_lvs(data):
|
|
|
|
lvs = []
|
|
|
|
for line in data.splitlines():
|
|
|
|
parts = line.strip().split(';')
|
|
|
|
lvs.append({
|
|
|
|
'name': parts[0],
|
2013-09-24 15:06:18 +02:00
|
|
|
'size': int(decimal_point.split(parts[1])[0]),
|
2013-02-27 11:24:56 +01:00
|
|
|
})
|
|
|
|
return lvs
|
|
|
|
|
2014-02-24 23:56:08 +01:00
|
|
|
|
2013-02-27 11:24:56 +01:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
2014-02-24 23:56:08 +01:00
|
|
|
argument_spec=dict(
|
2013-02-27 11:24:56 +01:00
|
|
|
vg=dict(required=True),
|
|
|
|
lv=dict(required=True),
|
|
|
|
size=dict(),
|
|
|
|
state=dict(choices=["absent", "present"], default='present'),
|
2014-01-26 18:07:48 +01:00
|
|
|
force=dict(type='bool', default='no'),
|
2013-02-27 11:24:56 +01:00
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
vg = module.params['vg']
|
|
|
|
lv = module.params['lv']
|
|
|
|
size = module.params['size']
|
|
|
|
state = module.params['state']
|
2014-01-26 18:07:48 +01:00
|
|
|
force = module.boolean(module.params['force'])
|
2013-07-19 10:59:11 +02:00
|
|
|
size_opt = 'L'
|
|
|
|
size_unit = 'm'
|
2013-02-27 11:24:56 +01:00
|
|
|
|
|
|
|
if size:
|
2013-06-24 22:37:31 +02:00
|
|
|
# LVCREATE(8) -l --extents option with percentage
|
|
|
|
if '%' in size:
|
2014-02-24 23:56:08 +01:00
|
|
|
size_parts = size.split('%', 1)
|
2013-06-24 22:37:31 +02:00
|
|
|
size_percent = int(size_parts[0])
|
|
|
|
if size_percent > 100:
|
|
|
|
module.fail_json(msg="Size percentage cannot be larger than 100%")
|
|
|
|
size_whole = size_parts[1]
|
|
|
|
if size_whole == 'ORIGIN':
|
|
|
|
module.fail_json(msg="Snapshot Volumes are not supported")
|
|
|
|
elif size_whole not in ['VG', 'PVS', 'FREE']:
|
|
|
|
module.fail_json(msg="Specify extents as a percentage of VG|PVS|FREE")
|
|
|
|
size_opt = 'l'
|
|
|
|
size_unit = ''
|
|
|
|
|
|
|
|
# LVCREATE(8) -L --size option unit
|
|
|
|
elif size[-1].isalpha():
|
2015-01-12 17:43:51 +01:00
|
|
|
if size[-1].lower() in 'bskmgtpe':
|
|
|
|
size_unit = size[-1].lower()
|
2013-06-24 22:37:31 +02:00
|
|
|
if size[0:-1].isdigit():
|
|
|
|
size = int(size[0:-1])
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Bad size specification for unit %s" % size_unit)
|
|
|
|
size_opt = 'L'
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Size unit should be one of [bBsSkKmMgGtTpPeE]")
|
|
|
|
# when no unit, megabytes by default
|
|
|
|
elif size.isdigit():
|
|
|
|
size = int(size)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Bad size specification")
|
|
|
|
|
|
|
|
if size_opt == 'l':
|
|
|
|
unit = 'm'
|
|
|
|
else:
|
|
|
|
unit = size_unit
|
2014-02-24 23:56:08 +01:00
|
|
|
|
2014-11-26 11:27:29 +01:00
|
|
|
lvs_cmd = module.get_bin_path("lvs", required=True)
|
2014-02-24 23:56:08 +01:00
|
|
|
rc, current_lvs, err = module.run_command(
|
2015-01-25 07:13:53 +01:00
|
|
|
"%s --noheadings --nosuffix -o lv_name,size --units %s --separator ';' %s" % (lvs_cmd, unit, vg))
|
2013-02-27 11:24:56 +01:00
|
|
|
|
|
|
|
if rc != 0:
|
2013-08-03 03:32:56 +02:00
|
|
|
if state == 'absent':
|
2014-02-24 23:56:08 +01:00
|
|
|
module.exit_json(changed=False, stdout="Volume group %s does not exist." % vg, stderr=False)
|
2013-08-03 03:32:56 +02:00
|
|
|
else:
|
2014-02-24 23:56:08 +01:00
|
|
|
module.fail_json(msg="Volume group %s does not exist." % vg, rc=rc, err=err)
|
2013-02-27 11:24:56 +01:00
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
lvs = parse_lvs(current_lvs)
|
|
|
|
|
|
|
|
for test_lv in lvs:
|
|
|
|
if test_lv['name'] == lv:
|
|
|
|
this_lv = test_lv
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
this_lv = None
|
|
|
|
|
2014-02-24 23:56:08 +01:00
|
|
|
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'])
|
|
|
|
|
2013-06-24 22:37:31 +02:00
|
|
|
msg = ''
|
2013-02-27 11:24:56 +01:00
|
|
|
if this_lv is None:
|
|
|
|
if state == 'present':
|
|
|
|
### create LV
|
|
|
|
if module.check_mode:
|
|
|
|
changed = True
|
|
|
|
else:
|
2014-11-26 11:27:29 +01:00
|
|
|
lvcreate_cmd = module.get_bin_path("lvcreate", required=True)
|
2015-04-08 18:33:04 +02:00
|
|
|
rc, _, err = module.run_command("%s -n %s -%s %s%s %s" % (lvcreate_cmd, lv, size_opt, size, size_unit, vg))
|
2013-02-27 11:24:56 +01:00
|
|
|
if rc == 0:
|
|
|
|
changed = True
|
|
|
|
else:
|
2014-01-26 18:07:48 +01:00
|
|
|
module.fail_json(msg="Creating logical volume '%s' failed" % lv, rc=rc, err=err)
|
2013-02-27 11:24:56 +01:00
|
|
|
else:
|
|
|
|
if state == 'absent':
|
|
|
|
### remove LV
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
2014-01-26 18:07:48 +01:00
|
|
|
if not force:
|
|
|
|
module.fail_json(msg="Sorry, no removal of logical volume %s without force=yes." % (this_lv['name']))
|
2014-11-26 11:27:29 +01:00
|
|
|
lvremove_cmd = module.get_bin_path("lvremove", required=True)
|
|
|
|
rc, _, err = module.run_command("%s --force %s/%s" % (lvremove_cmd, vg, this_lv['name']))
|
2013-02-27 11:24:56 +01:00
|
|
|
if rc == 0:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
else:
|
2014-02-24 23:56:08 +01:00
|
|
|
module.fail_json(msg="Failed to remove logical volume %s" % (lv), rc=rc, err=err)
|
2013-06-24 22:37:31 +02:00
|
|
|
|
|
|
|
elif size_opt == 'l':
|
|
|
|
module.exit_json(changed=False, msg="Resizing extents with percentage not supported.")
|
|
|
|
else:
|
|
|
|
### resize LV
|
|
|
|
tool = None
|
|
|
|
if size > this_lv['size']:
|
2014-11-26 11:27:29 +01:00
|
|
|
tool = module.get_bin_path("lvextend", required=True)
|
2013-06-24 22:37:31 +02:00
|
|
|
elif size < this_lv['size']:
|
2014-01-26 18:07:48 +01:00
|
|
|
if not force:
|
|
|
|
module.fail_json(msg="Sorry, no shrinking of %s without force=yes." % (this_lv['name']))
|
2014-11-26 11:27:29 +01:00
|
|
|
tool = module.get_bin_path("lvextend", required=True)
|
|
|
|
tool.append("--force")
|
2013-06-24 22:37:31 +02:00
|
|
|
|
|
|
|
if tool:
|
|
|
|
if module.check_mode:
|
2013-02-27 11:24:56 +01:00
|
|
|
changed = True
|
|
|
|
else:
|
2014-02-24 23:56:08 +01:00
|
|
|
rc, _, err = module.run_command("%s -%s %s%s %s/%s" % (tool, size_opt, size, size_unit, vg, this_lv['name']))
|
2013-06-24 22:37:31 +02:00
|
|
|
if rc == 0:
|
|
|
|
changed = True
|
2014-02-24 23:56:08 +01:00
|
|
|
elif "matches existing size" in err:
|
|
|
|
module.exit_json(changed=False, vg=vg, lv=this_lv['name'], size=this_lv['size'])
|
2013-06-24 22:37:31 +02:00
|
|
|
else:
|
2014-02-24 23:56:08 +01:00
|
|
|
module.fail_json(msg="Unable to resize %s to %s%s" % (lv, size, size_unit), rc=rc, err=err)
|
2013-02-27 11:24:56 +01:00
|
|
|
|
2014-02-24 23:56:08 +01:00
|
|
|
module.exit_json(changed=changed, msg=msg)
|
2013-02-27 11:24:56 +01:00
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2014-02-24 23:56:08 +01:00
|
|
|
|
2013-02-27 11:24:56 +01:00
|
|
|
main()
|