From 6caac2743a6c48c99f77838622b88f7652407347 Mon Sep 17 00:00:00 2001 From: Michael Scherer Date: Tue, 3 Oct 2017 21:26:25 +0200 Subject: [PATCH] Add support for LVM to filesystem module (#21620) And since LVM2_member is not a very userfiendly name, add a mechanism for more user friendly names. --- lib/ansible/modules/system/filesystem.py | 46 ++++++++++++++++++------ 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/lib/ansible/modules/system/filesystem.py b/lib/ansible/modules/system/filesystem.py index fce47875fb..d54e128964 100644 --- a/lib/ansible/modules/system/filesystem.py +++ b/lib/ansible/modules/system/filesystem.py @@ -23,9 +23,11 @@ description: version_added: "1.2" options: fstype: + choices: [ "ext4", "ext4dev", "ext3", "ext2", "xfs", "btrfs", "reiserfs", "lvm"] description: - File System type to be created. - reiserfs support was added in 2.2. + - lvm support was added in 2.4. required: true dev: description: @@ -111,22 +113,21 @@ def _get_fs_size(fssize_cmd, dev, module): # There is no way to get the blocksize and blockcount for btrfs filesystems block_size = 1 block_count = 1 - + elif 'pvs' == fssize_cmd: + rc, size, err = module.run_command([cmd, '--noheadings', '-o', 'pv_size', '--units', 'b', dev]) + if rc == 0: + block_count = int(size[:-1]) + block_size = 1 + else: + module.fail_json(msg="Failed to get block count and block size of %s with %s" % (dev, cmd), rc=rc, err=err ) return block_size*block_count def main(): - module = AnsibleModule( - argument_spec = dict( - fstype=dict(required=True, aliases=['type']), - dev=dict(required=True, aliases=['device']), - opts=dict(), - force=dict(type='bool', default='no'), - resizefs=dict(type='bool', default='no'), - ), - supports_check_mode=True, - ) + friendly_names = { + 'lvm': 'LVM2_member', + } # There is no "single command" to manipulate filesystems, so we map them all out and their options fs_cmd_map = { @@ -178,15 +179,38 @@ def main(): 'grow_flag' : 'filesystem resize', 'force_flag' : '-f', 'fsinfo': 'btrfs', + }, + 'LVM2_member' : { + 'mkfs' : 'pvcreate', + 'grow' : 'pvresize', + 'grow_flag' : None, + 'force_flag' : '-f' , + 'fsinfo': 'pvs', } } + module = AnsibleModule( + argument_spec = dict( + fstype=dict(required=True, aliases=['type'], + choices=fs_cmd_map.keys() + friendly_names.keys()), + dev=dict(required=True, aliases=['device']), + opts=dict(), + force=dict(type='bool', default='no'), + resizefs=dict(type='bool', default='no'), + ), + supports_check_mode=True, + ) + + dev = module.params['dev'] fstype = module.params['fstype'] opts = module.params['opts'] force = module.boolean(module.params['force']) resizefs = module.boolean(module.params['resizefs']) + if fstype in friendly_names: + fstype = friendly_names[fstype] + changed = False try: