2013-04-25 09:42:07 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, Alexander Bulimov <lazywolf0@gmail.com>
|
|
|
|
#
|
|
|
|
# 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
|
2013-05-06 14:08:38 +00:00
|
|
|
module: filesystem
|
2013-04-25 09:42:07 +00:00
|
|
|
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.
|
|
|
|
notes:
|
|
|
|
- uses mkfs command
|
|
|
|
'''
|
|
|
|
|
2013-06-14 09:53:43 +00:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Create a ext2 filesystem on /dev/sdb1.
|
|
|
|
- filesystem: fstype=ext2 dev=/dev/sdb1
|
|
|
|
|
|
|
|
# Create a ext4 filesystem on /dev/sdb1 and check disk blocks.
|
|
|
|
- filesystem: fstype=ext4 dev=/dev/sdb1 opts="-cc"
|
|
|
|
'''
|
|
|
|
|
2013-04-25 09:42:07 +00:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
2013-06-10 14:39:19 +00:00
|
|
|
fstype=dict(required=True, aliases=['type']),
|
|
|
|
dev=dict(required=True, aliases=['device']),
|
2013-04-25 09:42:07 +00:00
|
|
|
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)
|
|
|
|
|
2013-06-10 14:39:19 +00:00
|
|
|
cmd = module.get_bin_path('blkid', required=True)
|
|
|
|
|
|
|
|
rc,raw_fs,err = module.run_command("%s -o value -s TYPE %s" % (cmd, dev))
|
2013-04-25 09:42:07 +00:00
|
|
|
fs = raw_fs.strip()
|
|
|
|
|
|
|
|
|
|
|
|
if fs == fstype:
|
|
|
|
module.exit_json(changed=False)
|
|
|
|
elif fs and not force:
|
2013-05-11 18:03:40 +00:00
|
|
|
module.fail_json(msg="'%s' is already used as %s, use force=yes to overwrite"%(dev,fs), rc=rc, err=err)
|
2013-04-25 09:42:07 +00:00
|
|
|
|
|
|
|
### create fs
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
changed = True
|
|
|
|
else:
|
2013-05-30 17:09:11 +00:00
|
|
|
mkfs = module.get_bin_path('mkfs', required=True)
|
2013-04-25 09:42:07 +00:00
|
|
|
cmd = None
|
|
|
|
if opts is None:
|
2013-05-30 17:09:11 +00:00
|
|
|
cmd = "%s -t %s '%s'" % (mkfs, fstype, dev)
|
2013-04-25 09:42:07 +00:00
|
|
|
else:
|
2013-05-30 17:09:11 +00:00
|
|
|
cmd = "%s -t %s %s '%s'" % (mkfs, fstype, opts, dev)
|
2013-04-25 09:42:07 +00:00
|
|
|
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
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
main()
|