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

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.

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
1 Like

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.

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

1 Like

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.

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

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?

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”}

  - 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
1 Like

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.

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

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.

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