diff options
author | Jiri Tyr <jtyr@users.noreply.github.com> | 2018-08-14 02:13:25 +0100 |
---|---|---|
committer | Matt Davis <nitzmahone@users.noreply.github.com> | 2018-08-13 18:13:25 -0700 |
commit | 8a0fa4a3e39881eedc9fb2e2995684ab85446e37 (patch) | |
tree | 0503d208fcde338176f891d934ff4bff19bfcb05 | |
parent | 0854dc3f7fd63b846f707ee3c35778cabaf4cce6 (diff) | |
download | ansible-8a0fa4a3e39881eedc9fb2e2995684ab85446e37.tar.gz |
Fix for creation and removal of swap record in fstab (fixes #42706, #31437 and #30090) (#42837) (#43518)
-rw-r--r-- | changelogs/fragments/fix-swap-mount-module.yaml | 4 | ||||
-rw-r--r-- | lib/ansible/modules/system/mount.py | 16 | ||||
-rw-r--r-- | test/integration/targets/mount/tasks/main.yml | 100 |
3 files changed, 118 insertions, 2 deletions
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 2d36b6ac06..cf775256ad 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') |