Using Ansible replace to add discard and noatime options to entries in /etc/fstab for virtual machines
Took a bit of testing to figure this one out. These tasks will add the ‘discard’ and ‘noatime’ options to ext4 and xfs file systems in the /etc/fstab for VMware virtual linux machines. Will only add if the option doesn’t already exist in the line.
- name: add discard option for virtual machines
replace:
backup: yes
path: /etc/fstab
regexp: '^(\S+)(\s+)(\S+)(\s+)(ext4|xfs)(\s+)(((?!discard)\S)*)(\s+)(\d)(\s+)(\d)$'
replace: '\1\2\3\4\5\6\7,discard\9\10\11\12'
when: "ansible_system_vendor is search('VMware')"
- name: add noatime option for virtual machines
replace:
backup: yes
path: /etc/fstab
regexp: '^(\S+)(\s+)(\S+)(\s+)(ext4|xfs)(\s+)(((?!noatime)\S)*)(\s+)(\d)(\s+)(\d)$'
replace: '\1\2\3\4\5\6\7,noatime\9\10\11\12'
when: "ansible_system_vendor is search('VMware')"