#!/usr/bin/python # -*- coding: utf-8 -*- # (c) 2016, Fabrizio Colonna # # 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 . ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['preview'], 'supported_by': 'curated'} DOCUMENTATION = ''' --- author: - "Fabrizio Colonna (@ColOfAbRiX)" module: parted short_description: Configure block device partitions version_added: "2.3" description: - This module allows configuring block device partition using the C(parted) command line tool. For a full description of the fields and the options check the GNU parted manual. notes: - When fetching information about a new disk and when the version of parted installed on the system is before version 3.1, the module queries the kernel through C(/sys/) to obtain disk information. In this case the units CHS and CYL are not supported. requirements: - This module requires parted version 1.8.3 and above. - If the version of parted is below 3.1, it requires a Linux version running the sysfs file system C(/sys/). options: device: description: The block device (disk) where to operate. required: True align: description: Set alignment for newly created partitions. choices: ['none', 'cylinder', 'minimal', 'optimal'] default: optimal number: description: - The number of the partition to work with or the number of the partition that will be created. Required when performing any action on the disk, except fetching information. unit: description: - Selects the current default unit that Parted will use to display locations and capacities on the disk and to interpret those given by the user if they are not suffixed by an unit. When fetching information about a disk, it is always recommended to specify a unit. choices: [ 's', 'B', 'KB', 'KiB', 'MB', 'MiB', 'GB', 'GiB', 'TB', 'TiB', '%', 'cyl', 'chs', 'compact' ] default: KiB label: description: Creates a new disk label. choices: [ 'aix', 'amiga', 'bsd', 'dvh', 'gpt', 'loop', 'mac', 'msdos', 'pc98', 'sun', '' ] default: msdos part_type: description: - Is one of 'primary', 'extended' or 'logical' and may be specified only with 'msdos' or 'dvh' partition tables. A name must be specified for a 'gpt' partition table. Neither part-type nor name may be used with a 'sun' partition table. choices: ['primary', 'extended', 'logical'] part_start: description: - Where the partition will start as offset from the beginning of the disk, that is, the "distance" from the start of the disk. The distance can be specified with all the units supported by parted (except compat) and it is case sensitive. E.g. C(10GiB), C(15%). default: 0% part_end : description: - Where the partition will end as offset from the beginning of the disk, that is, the "distance" from the start of the disk. The distance can be specified with all the units supported by parted (except compat) and it is case sensitive. E.g. C(10GiB), C(15%). default: 100% name: description: - Sets the name for the partition number (GPT, Mac, MIPS and PC98 only). flags: description: A list of the flags that has to be set on the partition. state: description: - If to create or delete a partition. If set to C(info) the module will only return the device information. choices: ['present', 'absent', 'info'] default: info ''' RETURN = ''' partition_info: description: Current partition information returned: success type: dict contains: device: description: Generic device information. type: dict partitions: description: List of device partitions. type: list sample: > { "disk": { "dev": "/dev/sdb", "logical_block": 512, "model": "VMware Virtual disk", "physical_block": 512, "size": 5.0, "table": "msdos", "unit": "gib" }, "partitions": [{
.. _documentation-guide:

===================
Documentation Guide
===================

.. toctree::
   :maxdepth: 2

   documentation-guide
   include-documentation
   addendum
ule.run_command(command) if rc != 0: module.fail_json( msg="Error while running parted script: %s" % command.strip(), rc=rc, out=out, err=err ) def read_record(file_path, default=None): """ Reads the first line of a file and returns it. """ try: f = open(file_path, 'r') try: return f.readline().strip() finally: f.close() except IOError: return default def part_exists(partitions, attribute, number): """ Looks if a partition that has a specific value for a specific attribute actually exists. """ return any( part[attribute] and part[attribute] == number for part in partitions ) def check_size_format(size_str): """ Checks if the input string is an allowed size """ size, unit = parse_unit(size_str) return unit in parted_units def main(): global module, units_si, units_iec changed = False output_script = "" script = "" module = AnsibleModule( argument_spec={ 'device': {'required': True, 'type': 'str'}, 'align': { 'default': 'optimal', 'choices': ['none', 'cylinder', 'minimal', 'optimal'], 'type': 'str' }, 'number': {'default': None, 'type': 'int'}, # unit command 'unit': { 'default': 'KiB', 'choices': parted_units, 'type': 'str' }, # mklabel command 'label': { 'choices': [ 'aix', 'amiga', 'bsd', 'dvh', 'gpt', 'loop', 'mac', 'msdos', 'pc98', 'sun', '' ], 'type': 'str' }, # mkpart [] command 'part_type': { 'default': 'primary', 'choices': ['primary', 'extended', 'logical'], 'type': 'str' }, 'part_start': {'default': '0%', 'type': 'str'}, 'part_end': {'default': '100%', 'type': 'str'}, # name command 'name': {'type': 'str'}, # set command 'flags': {'type': 'list'}, # rm/mkpart command 'state': { 'choices': ['present', 'absent', 'info'], 'default': 'info', 'type': 'str' } }, supports_check_mode=True, ) # Data extraction device = module.params['device'] align = module.params['align'] number = module.params['number'] unit = module.params['unit'] label = module.params['label'] part_type = module.params['part_type'] part_start = module.params['part_start'] part_end = module.params['part_end'] name = module.params['name'] state = module.params['state'] flags = module.params['flags'] # Conditioning if number and number < 0: module.fail_json(msg="The partition number must be non negative.") if not check_size_format(part_start): module.fail_json( msg="The argument 'part_start' doesn't respect required format." "The size unit is case sensitive.", err=parse_unit(part_start) ) if not check_size_format(part_end): module.fail_json( msg="The argument 'part_end' doesn't respect required format." "The size unit is case sensitive.", err=parse_unit(part_end) ) # Read the current disk information current_device = get_device_info(device, unit) current_parts = current_device['partitions'] if state == 'present': # Default value for the label if not current_device['generic']['table'] or \ current_device['generic']['table'] == 'unknown' and \ not label: label = 'msdos' # Assign label if required if label: script += "mklabel %s " % label # Create partition if required if part_type and not part_exists(current_parts, 'num', number): script += "mkpart %s %s %s " % ( part_type, part_start, part_end ) # Set the unit of the run if unit and script: script = "unit %s %s" % (unit, script) # Execute the script and update the data structure. # This will create the partition for the next steps if script: output_script += script parted(script, device, align) changed = True script = "" current_parts = get_device_info(device, unit)['partitions'] if part_exists(current_parts, 'num', number) or module.check_mode: partition = {'flags': []} # Empty structure for the check-mode if not module.check_mode: partition = [p for p in current_parts if p['num'] == number][0] # Assign name to the the partition if name: script += "name %s %s " % (number, name) # Manage flags if flags: # Compute only the changes in flags status flags_off = list(set(partition['flags']) - set(flags)) flags_on = list(set(flags) - set(partition['flags'])) for f in flags_on: script += "set %s %s on " % (number, f) for f in flags_off: script += "set %s %s off " % (number, f) # Set the unit of the run if unit and script: script = "unit %s %s" % (unit, script) # Execute the script if script: output_script += script changed = True parted(script, device, align) elif state == 'absent': # Remove the partition if part_exists(current_parts, 'num', number) or module.check_mode: script = "rm %s " % number output_script += script changed = True parted(script, device, align) elif state == 'info': output_script = "unit '%s' print " % unit # Final status of the device final_device_status = get_device_info(device, unit) module.exit_json( changed=changed, disk=final_device_status['generic'], partitions=final_device_status['partitions'], script=output_script.strip() ) if __name__ == '__main__': main()