From dbc6963617714bfa317a6bdcda43c7091883c47b Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Mon, 11 Dec 2017 10:09:07 +0530 Subject: [PATCH] mount: Add option to backup fstab file (#33734) This fix adds option to create a backup of fstab file before making any changes to it. Signed-off-by: Abhijeet Kasurde --- lib/ansible/modules/system/mount.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/ansible/modules/system/mount.py b/lib/ansible/modules/system/mount.py index bb934d0d170..fa53fe51fd4 100644 --- a/lib/ansible/modules/system/mount.py +++ b/lib/ansible/modules/system/mount.py @@ -84,6 +84,14 @@ options: type: bool default: 'yes' version_added: '2.2' + backup: + description: + - Create a backup file including the timestamp information so you can get + the original file back if you somehow clobbered it incorrectly. + required: false + choices: [ "yes", "no" ] + default: "no" + version_added: '2.5' notes: - As of Ansible 2.3, the I(name) option has been changed to I(path) as default, but I(name) still works as well. @@ -125,7 +133,10 @@ from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native -def write_fstab(lines, path): +def write_fstab(module, lines, path): + if module.params['backup']: + module.backup_local(path) + fs_w = open(path, 'w') for l in lines: @@ -236,7 +247,7 @@ def set_mount(module, args): changed = True if changed and not module.check_mode: - write_fstab(to_write, args['fstab']) + write_fstab(module, to_write, args['fstab']) return (args['name'], changed) @@ -298,7 +309,7 @@ def unset_mount(module, args): changed = True if changed and not module.check_mode: - write_fstab(to_write, args['fstab']) + write_fstab(module, to_write, args['fstab']) return (args['name'], changed) @@ -548,6 +559,7 @@ def main(): opts=dict(type='str'), passno=dict(type='str'), src=dict(type='path'), + backup=dict(default=False, type='bool'), state=dict(type='str', required=True, choices=['absent', 'mounted', 'present', 'unmounted']), ), supports_check_mode=True,