From 2dc59822f770ce5f4cc7a2356f397d2469ebbe29 Mon Sep 17 00:00:00 2001 From: Alexander Bulimov Date: Thu, 25 Apr 2013 13:42:07 +0400 Subject: [PATCH 1/2] mkfs module to create filesystems with mkfs command --- library/mkfs | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100755 library/mkfs diff --git a/library/mkfs b/library/mkfs new file mode 100755 index 00000000000..718437e363d --- /dev/null +++ b/library/mkfs @@ -0,0 +1,106 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# (c) 2013, Alexander Bulimov +# +# 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 . + +DOCUMENTATION = ''' +--- +author: Alexander Bulimov +module: mkfs +short_description: Makes file system on block device +description: + - This module creates file system. +version_added: "1.2" +options: + fstype: + description: + - File System type to be created. + required: true + dev: + description: + - Target block device. + required: true + force: + choices: [ "yes", "no" ] + default: "no" + description: + - If yes, allows to create new filesystem on devices that already has filesystem. + required: false + opts: + description: + - List of options to be passed to mkfs command. +examples: + - description: Create a ext2 filesystem on /dev/sdb1. + code: mkfs fstype=ext2 dev=/dev/sdb1 + - description: Create a ext4 filesystem on /dev/sdb1 and check disk blocks. + code: mkfs fstype=ext4 dev=/dev/sdb1 opts="-cc" +notes: + - uses mkfs command +''' + +def main(): + module = AnsibleModule( + argument_spec = dict( + fstype=dict(required=True), + dev=dict(required=True), + opts=dict(), + force=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']) + + changed = False + + if not os.path.exists(dev): + module.fail_json(msg="Device %s not found."%dev) + + rc,raw_fs,err = module.run_command("blkid -o value -s TYPE %s"%(dev)) + fs = raw_fs.strip() + + + if fs == fstype: + module.exit_json(changed=False) + elif fs and not force: + module.fail_json(msg="'%s' is already used as %s, use force=yes to ignore"%(dev,fs), rc=rc, err=err) + + ### create fs + + if module.check_mode: + changed = True + else: + cmd = None + if opts is None: + cmd = "mkfs -t %s '%s'"%(fstype, dev) + else: + cmd = "mkfs -t %s %s '%s'"%(fstype, opts, dev) + rc,_,err = module.run_command(cmd) + if rc == 0: + changed = True + else: + module.fail_json(msg="Creating filesystem %s on device '%s' failed"%(fstype,dev), rc=rc, err=err) + + module.exit_json(changed=changed) + +# this is magic, see lib/ansible/module_common.py +#<> +main() From f4de40fc43a4e45f109d59f729790d51b49b9e6d Mon Sep 17 00:00:00 2001 From: Alexander Bulimov Date: Mon, 6 May 2013 18:08:38 +0400 Subject: [PATCH 2/2] renamed mkfs and moved to system/filesystem --- library/{mkfs => system/filesystem} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename library/{mkfs => system/filesystem} (95%) diff --git a/library/mkfs b/library/system/filesystem similarity index 95% rename from library/mkfs rename to library/system/filesystem index 718437e363d..5c7333bfea5 100755 --- a/library/mkfs +++ b/library/system/filesystem @@ -21,7 +21,7 @@ DOCUMENTATION = ''' --- author: Alexander Bulimov -module: mkfs +module: filesystem short_description: Makes file system on block device description: - This module creates file system. @@ -46,9 +46,9 @@ options: - List of options to be passed to mkfs command. examples: - description: Create a ext2 filesystem on /dev/sdb1. - code: mkfs fstype=ext2 dev=/dev/sdb1 + code: filesystem fstype=ext2 dev=/dev/sdb1 - description: Create a ext4 filesystem on /dev/sdb1 and check disk blocks. - code: mkfs fstype=ext4 dev=/dev/sdb1 opts="-cc" + code: filesystem fstype=ext4 dev=/dev/sdb1 opts="-cc" notes: - uses mkfs command '''