Question:
I am trying to add nodev to my /etc/fstab
file. I am using the Ansible command below but with no luck. My issue lies with the regular expression, I’m not a pro at regex.
1 2 3 4 5 6 7 8 9 |
- 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
is:
1 2 |
/dev/mapper/ex_sys-ex_home /home /ext4 rw,exec,auto,nouser,sync 1 2 |
Answer:
While this may not be the most elegant answer, it worked for me.
1 2 3 4 5 6 7 8 9 10 |
- 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 |