# Add fstab options and then only remount filesystems that have been changed

**URL:** <https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395>\
**Category:** Get Help\
**Tags:** loop\
**Created:** [January 19, 2024, 2:42pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395 "2024-01-19T14:42:29Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![wapaloosa](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@wapaloosa](https://forum.ansible.com/u/wapaloosa)\
**Post date:** [January 19, 2024, 2:42pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395/1 "2024-01-19T14:42:29Z")

</div>

This is my first post so please bear with me.  
I created a playbook that will loop through a list of mount points and if the mount points exist and correct options do not exist it will add the correct options. This part of the playbook works. I am having issues with the second task which is the shell module to mount -o remount only the filesystems that have changed.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [January 19, 2024, 4:46pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395/2 "2024-01-19T16:46:40Z")

</div>

You have several options, the 2 simplest:

- make it a handler and notify it from the first task
- use the mount module instead to do both operations in one shot

---

<div class="post-metadata">

**Author:** ![wapaloosa](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@wapaloosa](https://forum.ansible.com/u/wapaloosa)\
**Post date:** [January 19, 2024, 4:53pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395/3 "2024-01-19T16:53:16Z")

</div>

The issue is that i have over 80 mount points in the with\_items list. I am running this on an entire environment so if the mount point exists then it will update the options if needed. The problem I have been having is getting the remount to only happen on the filesystems that have been updated with an option. I have tried using handlers. the issue seems to be getting the correct output from mounts that have changed and only running shell command on those filesystems and they change between servers.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [January 19, 2024, 5:09pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395/4 "2024-01-19T17:09:46Z")

</div>

If using mount, that would be done inline, if using 2 tasks, register the results, have 2nd task/handler loop over register var and only mount the changed ones

---

<div class="post-metadata">

**Author:** ![wapaloosa](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@wapaloosa](https://forum.ansible.com/u/wapaloosa)\
**Post date:** [January 19, 2024, 5:51pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395/5 "2024-01-19T17:51:15Z")

</div>

Thats where the problem lies. I am having issues on the second task the mount task to mount only vars that changed from the first task. If I use the mount command then I have to list every src and path.

---

<div class="post-metadata">

**Author:** ![wapaloosa](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@wapaloosa](https://forum.ansible.com/u/wapaloosa)\
**Post date:** [January 19, 2024, 6:01pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395/6 "2024-01-19T18:01:53Z")

</div>

## here is an example of what I am trying to do. In reality I have 86 different mount points across all the servers.

- hosts: vm\_8  
become: true  
gather\_facts: false  
tasks:
  - name: add nodev to mount points  
lineinfile:  
path: /etc/fstab  
regexp: ‘^(\S+\s+{{ item }}\s+\S+\s+)(?!(?:\S\*,)?nodev(?:,\S\*)?\s+)(\S+)(\s+.+)$’  
line: ‘\1nodev,\2\3’  
with\_items:
    - /var/mount1
    - /usr/mount2
    - /var/mount3  
register: mounts

- name: remount filesystems that have been changed.  
shell: mount -o remount,rw {{ item }}  
when: mounts.changed

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [January 19, 2024, 6:29pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395/7 "2024-01-19T18:29:09Z")

</div>

The indentation is all wrong, but I will assume that is a copy/paste issue.

What is the output of running that playbook, can you post it? And expected/actual results?

---

<div class="post-metadata">

**Author:** ![wapaloosa](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@wapaloosa](https://forum.ansible.com/u/wapaloosa)\
**Post date:** [January 19, 2024, 6:38pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395/8 "2024-01-19T18:38:12Z")

</div>

the indentation is just from my cut and paste. the playbook runs it just does not remount only changed filesystems

output:  
TASK [mount changed filesystem] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [rhel8]: FAILED! =\> {“msg”: “The conditional check ‘mount.item.changed == true’ failed. The error was: error while evaluating conditional (mount.item.changed == true): ‘dict object’ has no attribute ‘item’. ‘dict object’ has no attribute ‘item’\n\nThe error appears to be in ‘/home/kmm/ansible/mount3.yml’: line 17, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n var: mount.changed\n - name: mount changed filesystem\n ^ here\n”}

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [January 19, 2024, 6:39pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395/9 "2024-01-19T18:39:14Z")

</div>

```auto
  - hosts: vm_8
    become: true
    gather_facts: false
    tasks:
    - name: add nodev to mount points
       lineinfile:
       path: /etc/fstab
       regexp: ‘^(\S+\s+{{ item }}\s+\S+\s+)(?!(?:\S*,)?nodev(?:,\S*)?\s+)(\S+)(\s+.+)$’
       line: ‘\1nodev,\2\3’
       with_items:
            /var/mount1
            /usr/mount2
            /var/mount3
      register: mounts

    - name: remount filesystems that have been changed.
      shell: mount -o remount,rw {{ item.<something, dont know lineinfile return that well> }}
      loop: '{{mounts.results}}'
      when: item is changed

```

---

<div class="post-metadata">

**Author:** ![wapaloosa](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@wapaloosa](https://forum.ansible.com/u/wapaloosa)\
**Post date:** [January 19, 2024, 6:44pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395/10 "2024-01-19T18:44:19Z")

</div>

Thanks I will give that a shot. lineinfile just looks for a string in the file and if it does not exist it will add it. The regex just looks for the line and string. that is part of my dilemma. Figuring out what variable to put in the mount -o remount.

---

<div class="post-metadata">

**Author:** ![wapaloosa](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@wapaloosa](https://forum.ansible.com/u/wapaloosa)\
**Post date:** [January 19, 2024, 7:02pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395/11 "2024-01-19T19:02:28Z")

</div>

the playbook works if I do one mount at a time but is not working when using the loop.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [January 19, 2024, 7:07pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395/12 "2024-01-19T19:07:49Z")

</div>

the difference with a loop is that you get a `results` list as the return with the normal return being an item (per loop item) in that list, in the same order as they were consumed in the task.

---

<div class="post-metadata">

**Author:** ![esherrill-uisg](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/esherrill-uisg/32/299_2.png) [@esherrill-uisg](https://forum.ansible.com/u/esherrill-uisg)\
**Post date:** [January 24, 2024, 1:16pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395/13 "2024-01-24T13:16:22Z")

</div>

Silly question: why not just remount all 80, regardless of whether changed or not?
