Repository: ansible/ansible

Issue:
Ansible module mount cannot remove specific swap devices/files from /etc/fstab
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
<!--- Explain the problem briefly -->
Ansible module `mount` cannot remove specific swap devices from `/etc/fstab`.

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
mount

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.13 (default, Nov 24 2017, 17:33:09) [GCC 6.3.0 20170516]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->
N/A

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
CentOS 7.5

##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->

The following task removes _all_ swap devices because it just matches `path` (should be `none` for swap devices according to fstab(5))

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: "Remove swap device(s) from /etc/fstab"
  mount:
    src: '{{ item }}'
    path: none
    fstype: swap
    backup: yes
    state: absent
  with_items: '{{ swapdevs.stdout_lines }}'
  when: 'swapdevs.stdout_lines |length > 0'
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Remove specified device only. To achieve that, the `src` device path should be matched instead of `path`.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
`mount` removes all swap device lines in `/etc/fstab`.

<!--- Paste verbatim command output between quotes below -->
```
changed: [n0107] => {
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "fstype": "swap", 
    "invocation": {
        "module_args": {
            "backup": true, 
            "boot": true, 
            "dump": null, 
            "fstab": null, 
            "fstype": "swap", 
            "opts": null, 
            "passno": null, 
            "path": "none", 
            "src": null, 
            "state": "absent"
        }
    }, 
    "name": "none", 
    "opts": "defaults", 
    "passno": "0"
}
```



Discussion:
Might be loosely related to #31437 and #30090.
cc @jtyr
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Thanks for opening this issue, @stephan2012. Please could you test the fix proposed in the PR #42837?
@jtyr: I'll try this week.
@stephan2012 Any luck with the testing?
@jtyr: Your fix works for me. Thanks for your support! :-)

Resolving patch:
diff --git a/lib/ansible/modules/system/mount.py b/lib/ansible/modules/system/mount.py
--- 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


Test patch:
diff --git a/test/integration/targets/mount/tasks/main.yml b/test/integration/targets/mount/tasks/main.yml
--- 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')