Inspired by Joe's answer, I made this version which will add a single option to a specific line in /etc/fstab if it is not already there. This will also save any other parameters that the line already had.
main.yml
- import_tasks: fstab-opt-present.yml point=/home opt=nodev
Fstab-manual present.yml
- name: '/etc/fstab: Set opt "{{ opt }}" for mount point {{ point }}' lineinfile: path: /etc/fstab backup: yes backrefs: yes regexp: '^(\S+\s+{{ point }}\s+\S+\s+)(?!(?:\S*,)?{{ opt }}(?:,\S*)?\s+)(\S+)(\s+.+)$' line: '\1{{ opt }},\2\3' register: fstab - name: 'If {{ point }} changed, remount' command: 'mount {{ point }} -o remount' when: fstab.changed
https://regex101.com/ is a really useful tool for creating and testing these types of regular expressions. Just turn on the multiline option and open the Replace panel, and you can even insert your /etc/fstab and see which lines will match your regular expression and what they will do with them. Remember to use real values โโinstead of Ansible {{ point }} variables, etc. When testing there
source share