Merge pull request #216 from mandar242/issues/126/mount-return-backup-file

Fix: Resolve merge conflict - mount return backup_file(#128)

SUMMARY

Resolves Merge conflict in #128 which Fixes #126

ISSUE TYPE


Feature Pull Request

COMPONENT NAME

mount
ADDITIONAL INFORMATION




Conflicting files in #128 
tests/integration/targets/mount/tasks/main.yml

Reviewed-by: Mike Graves <mgraves@redhat.com>
This commit is contained in:
ansible-zuul[bot] 2021-07-06 13:28:22 +00:00 committed by GitHub
commit e23ea12cc5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 142 additions and 148 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- mount - returns ``backup_file`` value when a backup fstab is created.

View file

@ -186,7 +186,6 @@ EXAMPLES = r'''
fstype: nfs fstype: nfs
''' '''
import errno import errno
import os import os
import platform import platform
@ -199,8 +198,11 @@ from ansible.module_utils.parsing.convert_bool import boolean
def write_fstab(module, lines, path): def write_fstab(module, lines, path):
if module.params['backup']: if module.params['backup']:
module.backup_local(path) backup_file = module.backup_local(path)
else:
backup_file = ""
fs_w = open(path, 'w') fs_w = open(path, 'w')
@ -210,6 +212,8 @@ def write_fstab(module, lines, path):
fs_w.flush() fs_w.flush()
fs_w.close() fs_w.close()
return backup_file
def _escape_fstab(v): def _escape_fstab(v):
"""Escape invalid characters in fstab fields. """Escape invalid characters in fstab fields.
@ -332,7 +336,7 @@ def _set_mount_save_old(module, args):
changed = True changed = True
if changed and not module.check_mode: if changed and not module.check_mode:
write_fstab(module, to_write, args['fstab']) args['backup_file'] = write_fstab(module, to_write, args['fstab'])
return (args['name'], old_lines, changed) return (args['name'], old_lines, changed)
@ -710,6 +714,7 @@ def main():
if platform.system() == 'FreeBSD': if platform.system() == 'FreeBSD':
args['opts'] = 'rw' args['opts'] = 'rw'
args['backup_file'] = ""
linux_mounts = [] linux_mounts = []
# Cache all mounts here in order we have consistent results if we need to # Cache all mounts here in order we have consistent results if we need to

View file

@ -126,7 +126,9 @@
- not dest_stat['stat']['exists'] - not dest_stat['stat']['exists']
when: ansible_system in ('FreeBSD', 'Linux') when: ansible_system in ('FreeBSD', 'Linux')
- name: Create fstab record for the first swap file - name: Block to test remounted option
block:
- name: Create fstab record for the first swap file
mount: mount:
name: none name: none
src: /tmp/swap1 src: /tmp/swap1
@ -134,9 +136,8 @@
fstype: swap fstype: swap
state: present state: present
register: swap1_created register: swap1_created
when: ansible_system in ('Linux')
- name: Try to create fstab record for the first swap file again - name: Try to create fstab record for the first swap file again
mount: mount:
name: none name: none
src: /tmp/swap1 src: /tmp/swap1
@ -144,16 +145,14 @@
fstype: swap fstype: swap
state: present state: present
register: swap1_created_again register: swap1_created_again
when: ansible_system in ('Linux')
- name: Check that we created the swap1 record - name: Check that we created the swap1 record
assert: assert:
that: that:
- swap1_created['changed'] - swap1_created['changed']
- not swap1_created_again['changed'] - not swap1_created_again['changed']
when: ansible_system in ('Linux')
- name: Create fstab record for the second swap file - name: Create fstab record for the second swap file
mount: mount:
name: none name: none
src: /tmp/swap2 src: /tmp/swap2
@ -161,9 +160,8 @@
fstype: swap fstype: swap
state: present state: present
register: swap2_created register: swap2_created
when: ansible_system in ('Linux')
- name: Try to create fstab record for the second swap file again - name: Try to create fstab record for the second swap file again
mount: mount:
name: none name: none
src: /tmp/swap1 src: /tmp/swap1
@ -171,70 +169,61 @@
fstype: swap fstype: swap
state: present state: present
register: swap2_created_again register: swap2_created_again
when: ansible_system in ('Linux')
- name: Check that we created the swap2 record - name: Check that we created the swap2 record
assert: assert:
that: that:
- swap2_created['changed'] - swap2_created['changed']
- not swap2_created_again['changed'] - not swap2_created_again['changed']
when: ansible_system in ('Linux')
- name: Remove the fstab record for the first swap file - name: Remove the fstab record for the first swap file
mount: mount:
name: none name: none
src: /tmp/swap1 src: /tmp/swap1
state: absent state: absent
register: swap1_removed register: swap1_removed
when: ansible_system in ('Linux')
- name: Try to remove the fstab record for the first swap file again - name: Try to remove the fstab record for the first swap file again
mount: mount:
name: none name: none
src: /tmp/swap1 src: /tmp/swap1
state: absent state: absent
register: swap1_removed_again register: swap1_removed_again
when: ansible_system in ('Linux')
- name: Check that we removed the swap1 record - name: Check that we removed the swap1 record
assert: assert:
that: that:
- swap1_removed['changed'] - swap1_removed['changed']
- not swap1_removed_again['changed'] - not swap1_removed_again['changed']
when: ansible_system in ('Linux')
- name: Remove the fstab record for the second swap file - name: Remove the fstab record for the second swap file
mount: mount:
name: none name: none
src: /tmp/swap2 src: /tmp/swap2
state: absent state: absent
register: swap2_removed register: swap2_removed
when: ansible_system in ('Linux')
- name: Try to remove the fstab record for the second swap file again - name: Try to remove the fstab record for the second swap file again
mount: mount:
name: none name: none
src: /tmp/swap2 src: /tmp/swap2
state: absent state: absent
register: swap2_removed_again register: swap2_removed_again
when: ansible_system in ('Linux')
- name: Check that we removed the swap2 record - name: Check that we removed the swap2 record
assert: assert:
that: that:
- swap2_removed['changed'] - swap2_removed['changed']
- not swap2_removed_again['changed'] - not swap2_removed_again['changed']
when: ansible_system in ('Linux')
- name: Create fstab record with missing last two fields - name: Create fstab record with missing last two fields
copy: copy:
dest: /etc/fstab dest: /etc/fstab
content: '//nas/photo /home/jik/pictures cifs defaults,credentials=/etc/security/nas.creds,uid=jik,gid=users,forceuid,forcegid,noserverino,_netdev content: '//nas/photo /home/jik/pictures cifs defaults,credentials=/etc/security/nas.creds,uid=jik,gid=users,forceuid,forcegid,noserverino,_netdev
' '
when: ansible_system in ('Linux')
- name: Try to change the fstab record with the missing last two fields - name: Try to change the fstab record with the missing last two fields
mount: mount:
src: //nas/photo src: //nas/photo
path: /home/jik/pictures path: /home/jik/pictures
@ -242,31 +231,24 @@
opts: defaults,credentials=/etc/security/nas.creds,uid=jik,gid=users,forceuid,forcegid,noserverino,_netdev,x-systemd.mount-timeout=0 opts: defaults,credentials=/etc/security/nas.creds,uid=jik,gid=users,forceuid,forcegid,noserverino,_netdev,x-systemd.mount-timeout=0
state: present state: present
register: optional_fields_update register: optional_fields_update
when: ansible_system in ('Linux')
- name: Get the content of the fstab file - name: Get the content of the fstab file
shell: cat /etc/fstab shell: cat /etc/fstab
register: optional_fields_content register: optional_fields_content
when: ansible_system in ('Linux')
- name: Check if the line containing the missing last two fields was changed - name: Check if the line containing the missing last two fields was changed
assert: assert:
that: that:
- optional_fields_update['changed'] - optional_fields_update['changed']
- ''' 0 0'' in optional_fields_content.stdout' - ''' 0 0'' in optional_fields_content.stdout'
- 1 == optional_fields_content.stdout_lines | length - 1 == optional_fields_content.stdout_lines | length
when: ansible_system in ('Linux')
- name: Block to test remounted option
block:
- name: Create empty file - name: Create empty file
community.general.filesize: community.general.filesize:
path: /tmp/myfs.img path: /tmp/myfs.img
size: 20M size: 20M
when: ansible_system in ('Linux')
- name: Format FS - name: Format FS
when: ansible_system in ('Linux')
community.general.filesystem: community.general.filesystem:
fstype: ext3 fstype: ext3
dev: /tmp/myfs.img dev: /tmp/myfs.img
@ -277,52 +259,58 @@
src: /tmp/myfs.img src: /tmp/myfs.img
fstype: ext2 fstype: ext2
state: mounted state: mounted
when: ansible_system in ('Linux')
- name: Get the last write time - name: Get the last write time
shell: 'dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i last write time: |cut -d: -f2-' shell: 'dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i last write time: |cut -d: -f2-'
register: last_write_time register: last_write_time
when: ansible_system in ('Linux')
- name: Wait 2 second - name: Wait 2 second
pause: pause:
seconds: 2 seconds: 2
when: ansible_system in ('Linux')
- name: Test if the FS is remounted - name: Test if the FS is remounted
mount: mount:
path: /tmp/myfs path: /tmp/myfs
state: remounted state: remounted
when: ansible_system in ('Linux')
- name: Get again the last write time - name: Get again the last write time
shell: 'dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i last write time: |cut -d: -f2-' shell: 'dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i last write time: |cut -d: -f2-'
register: last_write_time2 register: last_write_time2
when: ansible_system in ('Linux')
- name: Fail if they are the same - name: Fail if they are the same
fail: fail:
msg: Filesytem was not remounted, testing of the module failed! msg: Filesytem was not remounted, testing of the module failed!
when: last_write is defined and last_write_time2 is defined and last_write_time.stdout == last_write_time2.stdout and ansible_system in ('Linux') when: last_write is defined and last_write_time2 is defined and last_write_time.stdout == last_write_time2.stdout
- name: Remount filesystem with different opts using remounted option (Linux only) - name: Remount filesystem with different opts using remounted option (Linux only)
mount: mount:
path: /tmp/myfs path: /tmp/myfs
state: remounted state: remounted
opts: rw,noexec opts: rw,noexec
when: ansible_system == 'Linux'
- name: Get remounted options (Linux only) - name: Get remounted options (Linux only)
shell: mount | grep myfs | grep -E -w 'noexec' | wc -l shell: mount | grep myfs | grep -E -w 'noexec' | wc -l
register: remounted_options register: remounted_options
when: ansible_system == 'Linux'
- name: Make sure the filesystem now has the new opts after using remounted (Linux only) - name: Make sure the filesystem now has the new opts after using remounted (Linux only)
assert: assert:
that: that:
- "'1' in remounted_options.stdout" - "'1' in remounted_options.stdout"
- "1 == remounted_options.stdout_lines | length" - "1 == remounted_options.stdout_lines | length"
when: ansible_system == 'Linux'
- name: Mount the FS again to test backup
mount:
path: /tmp/myfs
src: /tmp/myfs.img
fstype: ext2
state: mounted
backup: yes
register: mount_backup_out
- name: ensure backup_file in returned output
assert:
that:
- "'backup_file' in mount_backup_out"
always: always:
- name: Umount the test FS - name: Umount the test FS
@ -331,7 +319,6 @@
src: /tmp/myfs.img src: /tmp/myfs.img
opts: loop opts: loop
state: absent state: absent
when: ansible_system in ('Linux')
- name: Remove the test FS - name: Remove the test FS
file: file: