2013-03-11 10:17:36 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, Alexander Bulimov <lazywolf0@gmail.com>
|
|
|
|
# based on lvol module by Jeroen Hoekx <jeroen.hoekx@dsquare.be>
|
|
|
|
#
|
|
|
|
# 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 = '''
|
|
|
|
---
|
|
|
|
author: Alexander Bulimov
|
|
|
|
module: lvg
|
|
|
|
short_description: Configure LVM volume groups
|
|
|
|
description:
|
2013-03-12 11:06:39 +01:00
|
|
|
- This module creates, removes or resizes volume groups.
|
2013-03-11 10:17:36 +01:00
|
|
|
version_added: "1.1"
|
|
|
|
options:
|
|
|
|
vg:
|
|
|
|
description:
|
|
|
|
- The name of the volume group.
|
|
|
|
required: true
|
|
|
|
dev:
|
|
|
|
description:
|
|
|
|
- List of comma-separated devices to use in this volume group.
|
|
|
|
required: true
|
2013-03-12 11:06:39 +01:00
|
|
|
pesize:
|
|
|
|
description:
|
|
|
|
- The size of the physical extent in megabytes.
|
|
|
|
default: 4
|
|
|
|
required: false
|
2013-03-11 10:17:36 +01:00
|
|
|
state:
|
|
|
|
choices: [ "present", "absent" ]
|
|
|
|
default: present
|
|
|
|
description:
|
|
|
|
- Control if the volume group exists.
|
|
|
|
required: false
|
|
|
|
force:
|
|
|
|
choices: [ "yes", "no" ]
|
2013-03-12 11:06:39 +01:00
|
|
|
default: "no"
|
2013-03-11 10:17:36 +01:00
|
|
|
description:
|
|
|
|
- If yes, allows to remove volume group with logical volumes.
|
|
|
|
required: false
|
|
|
|
examples:
|
2013-03-12 11:06:39 +01:00
|
|
|
- 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
|
2013-03-11 10:17:36 +01:00
|
|
|
- description: Create a volume group on top of /dev/sdb1 and /dev/sdc5.
|
|
|
|
code: lvg vg=vg.services dev=/dev/sdb1,/dev/sdc5
|
|
|
|
- description: Remove a volume group with name vg.services.
|
|
|
|
code: lvg vg=vg.services state=absent
|
|
|
|
notes:
|
2013-03-12 11:06:39 +01:00
|
|
|
- module does not modify PE size for already present volume group
|
2013-03-11 10:17:36 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
def parse_vgs(data):
|
|
|
|
vgs = []
|
|
|
|
for line in data.splitlines():
|
|
|
|
parts = line.strip().split(';')
|
|
|
|
vgs.append({
|
|
|
|
'name': parts[0],
|
|
|
|
'pv_count': int(parts[1]),
|
|
|
|
'lv_count': int(parts[2]),
|
|
|
|
})
|
|
|
|
return vgs
|
|
|
|
|
2013-03-12 11:06:39 +01:00
|
|
|
def parse_pvs(data):
|
|
|
|
pvs = []
|
|
|
|
for line in data.splitlines():
|
|
|
|
parts = line.strip().split(';')
|
|
|
|
pvs.append({
|
|
|
|
'name': parts[0],
|
|
|
|
'vg_name': parts[1],
|
|
|
|
})
|
|
|
|
return pvs
|
|
|
|
|
2013-03-11 10:17:36 +01:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
vg=dict(required=True),
|
|
|
|
dev=dict(),
|
2013-03-12 11:06:39 +01:00
|
|
|
pesize=dict(),
|
2013-03-11 10:17:36 +01:00
|
|
|
state=dict(choices=["absent", "present"], default='present'),
|
2013-03-12 11:06:39 +01:00
|
|
|
force=dict(type='bool', default='no'),
|
2013-03-11 10:17:36 +01:00
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
vg = module.params['vg']
|
2013-03-12 11:06:39 +01:00
|
|
|
state = module.params['state']
|
|
|
|
force = module.boolean(module.params['force'])
|
|
|
|
if module.params['pesize']:
|
|
|
|
pesize = int(module.params['pesize'])
|
|
|
|
else:
|
|
|
|
pesize = 4
|
|
|
|
|
2013-03-11 10:17:36 +01:00
|
|
|
if module.params['dev']:
|
|
|
|
dev = module.params['dev'].replace(',',' ')
|
|
|
|
dev_list = module.params['dev'].split(',')
|
2013-03-12 11:06:39 +01:00
|
|
|
elif state == 'present':
|
2013-03-11 10:17:36 +01:00
|
|
|
module.fail_json(msg="No dev given.")
|
|
|
|
|
|
|
|
if state=='present':
|
2013-03-12 11:06:39 +01:00
|
|
|
### check given devices
|
2013-03-11 10:17:36 +01:00
|
|
|
for test_dev in dev_list:
|
|
|
|
if not os.path.exists(test_dev):
|
|
|
|
module.fail_json(msg="No dev %s found."%test_dev)
|
|
|
|
|
2013-03-12 11:06:39 +01:00
|
|
|
### get pv list
|
|
|
|
rc,current_pvs,err = module.run_command("pvs --noheadings -o pv_name,vg_name --separator ';'")
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="Failed executing pvs command.",rc=rc, err=err)
|
|
|
|
|
|
|
|
### 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)
|
|
|
|
if used_pvs:
|
|
|
|
module.fail_json(msg="dev %s is already in %s volume group."%(used_pvs[0]['name'],used_pvs[0]['vg_name']))
|
|
|
|
|
2013-03-11 10:17:36 +01:00
|
|
|
rc,current_vgs,err = module.run_command("vgs --noheadings -o vg_name,pv_count,lv_count --separator ';'")
|
|
|
|
|
|
|
|
if rc != 0:
|
2013-03-12 11:06:39 +01:00
|
|
|
module.fail_json(msg="Failed executing vgs command.",rc=rc, err=err)
|
2013-03-11 10:17:36 +01:00
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
vgs = parse_vgs(current_vgs)
|
|
|
|
|
|
|
|
for test_vg in vgs:
|
|
|
|
if test_vg['name'] == vg:
|
|
|
|
this_vg = test_vg
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
this_vg = None
|
|
|
|
|
|
|
|
if this_vg is None:
|
|
|
|
if state == 'present':
|
|
|
|
### create VG
|
|
|
|
if module.check_mode:
|
|
|
|
changed = True
|
|
|
|
else:
|
2013-03-12 11:06:39 +01:00
|
|
|
### create PV
|
|
|
|
for current_dev in dev_list:
|
|
|
|
rc,_,err = module.run_command("pvcreate %s"%current_dev)
|
|
|
|
if rc == 0:
|
|
|
|
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))
|
2013-03-11 10:17:36 +01:00
|
|
|
if rc == 0:
|
|
|
|
changed = True
|
|
|
|
else:
|
2013-03-12 11:06:39 +01:00
|
|
|
module.fail_json(msg="Creating volume group '%s' failed"%vg, rc=rc, err=err)
|
2013-03-11 10:17:36 +01:00
|
|
|
else:
|
|
|
|
if state == 'absent':
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
else:
|
2013-03-12 11:06:39 +01:00
|
|
|
if this_vg['lv_count'] == 0 or force:
|
2013-03-11 10:17:36 +01:00
|
|
|
### remove VG
|
|
|
|
rc,_,err = module.run_command("vgremove --force %s"%(vg))
|
|
|
|
if rc == 0:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Failed to remove volume group %s"%(vg),rc=rc, err=err)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Refuse to remove non-empty volume group %s without force=yes"%(vg))
|
|
|
|
|
2013-03-12 11:06:39 +01:00
|
|
|
### resize VG
|
|
|
|
action = None
|
|
|
|
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_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 module.check_mode:
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
if action == 'extend' or action == 'modify':
|
|
|
|
tool = 'vgextend'
|
|
|
|
### create PV
|
|
|
|
for current_dev in devs_to_add:
|
|
|
|
rc,_,err = module.run_command("pvcreate %s"%current_dev)
|
|
|
|
if rc == 0:
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Creating physical volume '%s' failed"%current_dev, rc=rc, err=err)
|
|
|
|
else:
|
|
|
|
tool = 'vgreduce --force'
|
|
|
|
|
|
|
|
### 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))
|
|
|
|
if rc == 0:
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Unable to reduce %s by %s."%(vg, devs_to_remove_string),rc=rc,err=err)
|
|
|
|
|
2013-03-11 10:17:36 +01:00
|
|
|
module.exit_json(changed=changed)
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
main()
|