mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
254 lines
9.2 KiB
Python
254 lines
9.2 KiB
Python
12 years ago
|
#!/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:
|
||
12 years ago
|
- This module creates, removes or resizes volume groups.
|
||
12 years ago
|
version_added: "1.1"
|
||
|
options:
|
||
|
vg:
|
||
|
description:
|
||
|
- The name of the volume group.
|
||
|
required: true
|
||
12 years ago
|
pvs:
|
||
12 years ago
|
description:
|
||
12 years ago
|
- List of comma-separated devices to use as physical devices in this volume group. Required when creating or resizing volume group.
|
||
|
required: false
|
||
12 years ago
|
pesize:
|
||
|
description:
|
||
12 years ago
|
- The size of the physical extent in megabytes. Must be a power of 2.
|
||
12 years ago
|
default: 4
|
||
|
required: false
|
||
11 years ago
|
vg_options:
|
||
11 years ago
|
description:
|
||
|
- Additional options to pass to C(vgcreate) when creating the volume group.
|
||
|
default: null
|
||
|
required: false
|
||
11 years ago
|
version_added: "1.6"
|
||
12 years ago
|
state:
|
||
|
choices: [ "present", "absent" ]
|
||
|
default: present
|
||
|
description:
|
||
|
- Control if the volume group exists.
|
||
|
required: false
|
||
|
force:
|
||
|
choices: [ "yes", "no" ]
|
||
12 years ago
|
default: "no"
|
||
12 years ago
|
description:
|
||
12 years ago
|
- If yes, allows to remove volume group with logical volumes.
|
||
12 years ago
|
required: false
|
||
|
notes:
|
||
12 years ago
|
- module does not modify PE size for already present volume group
|
||
12 years ago
|
'''
|
||
|
|
||
12 years ago
|
EXAMPLES = '''
|
||
|
# Create a volume group on top of /dev/sda1 with physical extent size = 32MB.
|
||
|
- lvg: vg=vg.services pvs=/dev/sda1 pesize=32
|
||
|
|
||
11 years ago
|
# Create or resize a volume group on top of /dev/sdb1 and /dev/sdc5.
|
||
12 years ago
|
# If, for example, we already have VG vg.services on top of /dev/sdb1,
|
||
|
# this VG will be extended by /dev/sdc5. Or if vg.services was created on
|
||
|
# top of /dev/sda5, we first extend it with /dev/sdb1 and /dev/sdc5,
|
||
|
# and then reduce by /dev/sda5.
|
||
|
- lvg: vg=vg.services pvs=/dev/sdb1,/dev/sdc5
|
||
|
|
||
|
# Remove a volume group with name vg.services.
|
||
|
- lvg: vg=vg.services state=absent
|
||
|
'''
|
||
|
|
||
12 years ago
|
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
|
||
|
|
||
11 years ago
|
def find_mapper_device_name(module, dm_device):
|
||
|
dmsetup_cmd = module.get_bin_path('dmsetup', True)
|
||
|
mapper_prefix = '/dev/mapper/'
|
||
|
rc, dm_name, err = module.run_command("%s info -C --noheadings -o name %s" % (dmsetup_cmd, dm_device))
|
||
|
if rc != 0:
|
||
|
module.fail_json(msg="Failed executing dmsetup command.", rc=rc, err=err)
|
||
|
mapper_device = mapper_prefix + dm_name.rstrip()
|
||
|
return mapper_device
|
||
|
|
||
|
def parse_pvs(module, data):
|
||
12 years ago
|
pvs = []
|
||
11 years ago
|
dm_prefix = '/dev/dm-'
|
||
12 years ago
|
for line in data.splitlines():
|
||
|
parts = line.strip().split(';')
|
||
11 years ago
|
if parts[0].startswith(dm_prefix):
|
||
|
parts[0] = find_mapper_device_name(module, parts[0])
|
||
12 years ago
|
pvs.append({
|
||
|
'name': parts[0],
|
||
|
'vg_name': parts[1],
|
||
|
})
|
||
|
return pvs
|
||
|
|
||
12 years ago
|
def main():
|
||
|
module = AnsibleModule(
|
||
|
argument_spec = dict(
|
||
|
vg=dict(required=True),
|
||
12 years ago
|
pvs=dict(type='list'),
|
||
|
pesize=dict(type='int', default=4),
|
||
11 years ago
|
vg_options=dict(default=''),
|
||
12 years ago
|
state=dict(choices=["absent", "present"], default='present'),
|
||
12 years ago
|
force=dict(type='bool', default='no'),
|
||
12 years ago
|
),
|
||
|
supports_check_mode=True,
|
||
|
)
|
||
|
|
||
|
vg = module.params['vg']
|
||
12 years ago
|
state = module.params['state']
|
||
|
force = module.boolean(module.params['force'])
|
||
12 years ago
|
pesize = module.params['pesize']
|
||
11 years ago
|
vgoptions = module.params['vg_options'].split()
|
||
12 years ago
|
|
||
12 years ago
|
if module.params['pvs']:
|
||
|
dev_string = ' '.join(module.params['pvs'])
|
||
|
dev_list = module.params['pvs']
|
||
12 years ago
|
elif state == 'present':
|
||
12 years ago
|
module.fail_json(msg="No physical volumes given.")
|
||
12 years ago
|
|
||
11 years ago
|
|
||
11 years ago
|
|
||
12 years ago
|
if state=='present':
|
||
12 years ago
|
### check given devices
|
||
12 years ago
|
for test_dev in dev_list:
|
||
|
if not os.path.exists(test_dev):
|
||
12 years ago
|
module.fail_json(msg="Device %s not found."%test_dev)
|
||
12 years ago
|
|
||
12 years ago
|
### get pv list
|
||
11 years ago
|
pvs_cmd = module.get_bin_path('pvs', True)
|
||
|
rc,current_pvs,err = module.run_command("%s --noheadings -o pv_name,vg_name --separator ';'" % pvs_cmd)
|
||
12 years ago
|
if rc != 0:
|
||
|
module.fail_json(msg="Failed executing pvs command.",rc=rc, err=err)
|
||
|
|
||
|
### check pv for devices
|
||
11 years ago
|
pvs = parse_pvs(module, current_pvs)
|
||
12 years ago
|
used_pvs = [ pv for pv in pvs if pv['name'] in dev_list and pv['vg_name'] and pv['vg_name'] != vg ]
|
||
12 years ago
|
if used_pvs:
|
||
12 years ago
|
module.fail_json(msg="Device %s is already in %s volume group."%(used_pvs[0]['name'],used_pvs[0]['vg_name']))
|
||
12 years ago
|
|
||
11 years ago
|
vgs_cmd = module.get_bin_path('vgs', True)
|
||
|
rc,current_vgs,err = module.run_command("%s --noheadings -o vg_name,pv_count,lv_count --separator ';'" % vgs_cmd)
|
||
12 years ago
|
|
||
|
if rc != 0:
|
||
12 years ago
|
module.fail_json(msg="Failed executing vgs command.",rc=rc, err=err)
|
||
12 years ago
|
|
||
|
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:
|
||
12 years ago
|
### create PV
|
||
11 years ago
|
pvcreate_cmd = module.get_bin_path('pvcreate', True)
|
||
12 years ago
|
for current_dev in dev_list:
|
||
11 years ago
|
rc,_,err = module.run_command("%s %s" % (pvcreate_cmd,current_dev))
|
||
12 years ago
|
if rc == 0:
|
||
|
changed = True
|
||
|
else:
|
||
11 years ago
|
module.fail_json(msg="Creating physical volume '%s' failed" % current_dev, rc=rc, err=err)
|
||
11 years ago
|
vgcreate_cmd = module.get_bin_path('vgcreate')
|
||
11 years ago
|
rc,_,err = module.run_command([vgcreate_cmd] + vgoptions + ['-s', str(pesize), vg, dev_string])
|
||
12 years ago
|
if rc == 0:
|
||
|
changed = True
|
||
|
else:
|
||
12 years ago
|
module.fail_json(msg="Creating volume group '%s' failed"%vg, rc=rc, err=err)
|
||
12 years ago
|
else:
|
||
|
if state == 'absent':
|
||
|
if module.check_mode:
|
||
|
module.exit_json(changed=True)
|
||
|
else:
|
||
12 years ago
|
if this_vg['lv_count'] == 0 or force:
|
||
12 years ago
|
### remove VG
|
||
11 years ago
|
vgremove_cmd = module.get_bin_path('vgremove', True)
|
||
|
rc,_,err = module.run_command("%s --force %s" % (vgremove_cmd, vg))
|
||
12 years ago
|
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))
|
||
|
|
||
12 years ago
|
### resize VG
|
||
12 years ago
|
current_devs = [ pv['name'] for pv in pvs if pv['vg_name'] == vg ]
|
||
12 years ago
|
devs_to_remove = list(set(current_devs) - set(dev_list))
|
||
|
devs_to_add = list(set(dev_list) - set(current_devs))
|
||
12 years ago
|
|
||
|
if devs_to_add or devs_to_remove:
|
||
12 years ago
|
if module.check_mode:
|
||
|
changed = True
|
||
|
else:
|
||
12 years ago
|
if devs_to_add:
|
||
|
devs_to_add_string = ' '.join(devs_to_add)
|
||
12 years ago
|
### create PV
|
||
11 years ago
|
pvcreate_cmd = module.get_bin_path('pvcreate', True)
|
||
12 years ago
|
for current_dev in devs_to_add:
|
||
11 years ago
|
rc,_,err = module.run_command("%s %s" % (pvcreate_cmd, current_dev))
|
||
12 years ago
|
if rc == 0:
|
||
|
changed = True
|
||
|
else:
|
||
|
module.fail_json(msg="Creating physical volume '%s' failed"%current_dev, rc=rc, err=err)
|
||
12 years ago
|
### add PV to our VG
|
||
11 years ago
|
vgextend_cmd = module.get_bin_path('vgextend', True)
|
||
11 years ago
|
rc,_,err = module.run_command("%s %s %s" % (vgextend_cmd, vg, devs_to_add_string))
|
||
12 years ago
|
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)
|
||
12 years ago
|
|
||
12 years ago
|
### remove some PV from our VG
|
||
|
if devs_to_remove:
|
||
|
devs_to_remove_string = ' '.join(devs_to_remove)
|
||
11 years ago
|
vgreduce_cmd = module.get_bin_path('vgreduce', True)
|
||
|
rc,_,err = module.run_command("%s --force %s %s" % (vgreduce_cmd, vg, devs_to_remove_string))
|
||
12 years ago
|
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)
|
||
|
|
||
12 years ago
|
module.exit_json(changed=changed)
|
||
|
|
||
11 years ago
|
# import module snippets
|
||
11 years ago
|
from ansible.module_utils.basic import *
|
||
12 years ago
|
main()
|