mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 13:22:48 +00:00
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 <akasurde@redhat.com>
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user