Adding the fstab option using Ansible

I am trying to add nodev to my /etc/fstab . I use the Ansible command below, but no luck. My problem is regular expression, I am not a regular expression professional.

 - name: Add nodev to /etc/fstab lineinfile: dest=/etc/fstab backup=yes backrefs=yes state=present regexp='(^/dev[\w/_-]+(\s+(?!nodev)[\w,]+)*)' line='\1,nodev' 

One of the lines from /etc/fstab that I am trying to add nodev :

 /dev/mapper/ex_sys-ex_home /home /ext4 rw,exec,auto,nouser,sync 1 2 
+7
source share
6 answers

Although this may not be the most elegant answer, it worked for me.

 - name: Ensure fstab uses nodev mount: name: "{{ item.mount }}" src: "{{ item.device }}" fstype: "{{ item.fstype }}" opts: "{{ item.options }},nodev" state: present with_items: ansible_mounts when: item.options.find(",") >= 0 and item.options.find("nodev") == -1 
+14
source

We have developed a third-party module for adding, installing or removing mount options. Check this!

  - mountopts: name: / option: nodev 

https://github.com/Uberspace/ansible-mountopts

+2
source

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

+2
source

Landing here in search of an answer, he launched my own for my use:

main.yml

 - include: fstab-opts.yml point=/tmp opts=noexec,nodev,nosuid,noatime - include: fstab-opts.yml point=/backup opts=noatime 

Fstab-opts.yml

 --- - name: 'Ensure {{ point }} flags' lineinfile: path: /etc/fstab # uses "(not-spaces spaces /tmp spaces )(not-spaces)(the rest)" pattern to match column content and capture args regexp: '^([^ ]+[ ]+\{{ point }}[ ]+[^ ]+[ ]+)([^ ]+)(.*)' line: '\1{{ opts }}\3' backrefs: yes register: fstab - name: 'If {{ point }} changed, remount' command: mount -o remount {{ point }} when: fstab.changed 
0
source

Tested and works great

  • name: set the nodev replace parameter: path: / etc / fstab backup: yes regex: '^ (\ S + \ S +) (/ \ S +) (\ S +) ((?: ext4 | xfs)) \ S +) (? ! (?: \ S *,)? Nodev (?:, \ S *)? \ S +) (\ S +) (\ S +. +) $ 'Replace:' \ 1 \ 2 \ 4 \ 5, nodev \ 6 ''

This eliminates the addition of nodev to / (root), installs only for ext4 and xfs file systems. does not add to temporary file systems.

Note: when testing regexp101, be sure to select python

0
source

I wanted to state that it seems that a new ANSIBLE module has appeared, which covers all this much easier: https://docs.ansible.com/ansible/latest/modules/mount_module.html.

0
source

Source: https://habr.com/ru/post/975658/


All Articles