From c93f24b45bb0dfe7703cc7757d84d12af9ff053e Mon Sep 17 00:00:00 2001 From: Jiri Tyr Date: Tue, 31 Jul 2018 22:09:38 +0100 Subject: [PATCH] Fix for creation and removal of swap record in fstab (fixes #42706, #31437 and #30090) (#42837) --- .../fragments/fix-swap-mount-module.yaml | 4 + lib/ansible/modules/system/mount.py | 16 ++- test/integration/targets/mount/tasks/main.yml | 100 ++++++++++++++++++ 3 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/fix-swap-mount-module.yaml diff --git a/changelogs/fragments/fix-swap-mount-module.yaml b/changelogs/fragments/fix-swap-mount-module.yaml new file mode 100644 index 0000000000..1aae36a58e --- /dev/null +++ b/changelogs/fragments/fix-swap-mount-module.yaml @@ -0,0 +1,4 @@ +--- +bugfixes: +- Fix the mount module's handling of swap entries in fstab + (https://github.com/ansible/ansible/pull/42837) diff --git a/lib/ansible/modules/system/mount.py b/lib/ansible/modules/system/mount.py index f557863c57..4bc9d43d9c 100644 --- a/lib/ansible/modules/system/mount.py +++ b/lib/ansible/modules/system/mount.py @@ -218,7 +218,13 @@ def set_mount(module, args): ) = line.split() # Check if we found the correct line - if ld['name'] != escaped_args['name']: + if ( + ld['name'] != escaped_args['name'] or ( + # In the case of swap, check the src instead + 'src' in args and + ld['name'] == 'none' and + ld['fstype'] == 'swap' and + ld['src'] != args['src'])): to_write.append(line) continue @@ -299,7 +305,13 @@ def unset_mount(module, args): ld['passno'] ) = line.split() - if ld['name'] != escaped_name: + if ( + ld['name'] != escaped_name or ( + # In the case of swap, check the src instead + 'src' in args and + ld['name'] == 'none' and + ld['fstype'] == 'swap' and + ld['src'] != args['src'])): to_write.append(line) continue diff --git a/test/integration/targets/mount/tasks/main.yml b/test/integration/targets/mount/tasks/main.yml index b4268deb0f..6fdff8aac5 100644 --- a/test/integration/targets/mount/tasks/main.yml +++ b/test/integration/targets/mount/tasks/main.yml @@ -148,3 +148,103 @@ - "unmount_result['changed']" - "not dest_stat['stat']['exists']" when: ansible_system in ('FreeBSD', 'Linux') + +- name: Create fstab record for the first swap file + mount: + name: none + src: /tmp/swap1 + opts: sw + fstype: swap + state: present + register: swap1_created + when: ansible_system in ('Linux') + +- name: Try to create fstab record for the first swap file again + mount: + name: none + src: /tmp/swap1 + opts: sw + fstype: swap + state: present + register: swap1_created_again + when: ansible_system in ('Linux') + +- name: Check that we created the swap1 record + assert: + that: + - "swap1_created['changed']" + - "not swap1_created_again['changed']" + when: ansible_system in ('Linux') + +- name: Create fstab record for the second swap file + mount: + name: none + src: /tmp/swap2 + opts: sw + fstype: swap + state: present + register: swap2_created + when: ansible_system in ('Linux') + +- name: Try to create fstab record for the second swap file again + mount: + name: none + src: /tmp/swap1 + opts: sw + fstype: swap + state: present + register: swap2_created_again + when: ansible_system in ('Linux') + +- name: Check that we created the swap2 record + assert: + that: + - "swap2_created['changed']" + - "not swap2_created_again['changed']" + when: ansible_system in ('Linux') + +- name: Remove the fstab record for the first swap file + mount: + name: none + src: /tmp/swap1 + state: absent + register: swap1_removed + when: ansible_system in ('Linux') + +- name: Try to remove the fstab record for the first swap file again + mount: + name: none + src: /tmp/swap1 + state: absent + register: swap1_removed_again + when: ansible_system in ('Linux') + +- name: Check that we removed the swap1 record + assert: + that: + - "swap1_removed['changed']" + - "not swap1_removed_again['changed']" + when: ansible_system in ('Linux') + +- name: Remove the fstab record for the second swap file + mount: + name: none + src: /tmp/swap2 + state: absent + register: swap2_removed + when: ansible_system in ('Linux') + +- name: Try to remove the fstab record for the second swap file again + mount: + name: none + src: /tmp/swap2 + state: absent + register: swap2_removed_again + when: ansible_system in ('Linux') + +- name: Check that we removed the swap2 record + assert: + that: + - "swap2_removed['changed']" + - "not swap2_removed_again['changed']" + when: ansible_system in ('Linux')