def testAddRelatimeMountOption(self): fs = fstab.Fstab() fs.read(StringIO.StringIO("/dev / ext3 defaults 0 1\n")) for line in fs.lines: if line.has_filesystem() and "relatime" not in line.options: line.options += ["relatime"] f = StringIO.StringIO() fs.write(f) self.assertEqual(f.getvalue(), "/dev / ext3 defaults,relatime 0 1\n")
def main(): module = AnsibleModule(argument_spec=dict( fstab=dict(default='/etc/fstab', type='path'), name=dict(required=True, type='path'), state=dict(default='present', choices=['present', 'absent']), option=dict(required=True), value=dict(), )) if not HAS_FSTAB_LIB: module.fail_json(msg='missing the `fstab` python module') if not os.path.exists(module.params['fstab']): module.fail_json(msg='given fstab file does not exist: ' + module.params['fstab']) changed = None ft = fstab.Fstab() ft.read(module.params['fstab']) line = find_mount(ft, module.params['name']) if not line: odule.fail_json( msg= 'given mountpoint does not exist: {}. You can create it using the mount-module.' .format(module.params['name'])) opts = parse_options(line.get_options()) if module.params['state'] == 'present': if module.params['value']: changed = (opts.get(module.params['option'], None) != module.params['value']) opts[module.params['option']] = module.params['value'] else: changed = (module.params['option'] not in opts) opts[module.params['option']] = None elif module.params['state'] == 'absent': if module.params['option'] in opts: del opts[module.params['option']] changed = True else: changed = False else: raise module.fail_json(msg='parameter "state": unknown value') line.set_options(dump_options(opts)) ft.write(module.params['fstab']) if changed is None: raise module.fail_json(msg='bug: no changed value was set') module.exit_json(changed=changed)
import sys import json import fstab fstabPath = '/etc/fstab' def row(rowName): row = [] row.append('LABEL=') row.append(rowName) row.append(' none ntfs rw,auto,nobrowse') return row.join('') for v in sys.argv[1:]: if v == 'readFsTab': lines = [] fs = fstab.Fstab() fs.read(fstabPath) for line in fs.lines: lines.append(line.get_raw().split(' ')[0].split('=')[1]) print('-'.join(lines)) if v == 'addToFsTab': print('f**k add') if v == 'deleteFsTabItem': print('f**k delete')
def setUp(self): self.fstab = fstab.Fstab() self.fstab.read(StringIO.StringIO(self.content))