1

I want to add the word "nodev" to the line that contain /home in /etc/fstab file
here's the file before

UUID=2ea6e2e5-d902-4289-b516-40c175cd481b /                       ext3    defaults        1 1
UUID=114107f7-5b48-44b7-ad38-09b47b1d6b02 /boot                   ext3    defaults        1 2
UUID=830757d1-60ad-4f4a-b41a-cad04d309e9d /home                   ext3    defaults        1 2
UUID=4b309559-2391-4f28-b2f5-38029e278db0 /tmp                    ext3    defaults        1 2
UUID=1dfc4cab-4de4-491f-8bd0-3503e9027717 /var                    xfs     defaults        0 0
UUID=f0846773-528b-42a2-a0dc-7a736595f7e1 /var/log                xfs     defaults        0 0
UUID=25ef70f1-7083-4a9b-a64c-970f5d4ad742 /var/log/audit          xfs     defaults        0 0
UUID=40d2f177-aa5c-43a1-9d85-e21bf2bafb39 swap                    swap    defaults        0 0

Here's what i want to have

UUID=2ea6e2e5-d902-4289-b516-40c175cd481b /                       ext3    defaults        1 1
UUID=114107f7-5b48-44b7-ad38-09b47b1d6b02 /boot                   ext3    defaults        1 2
UUID=830757d1-60ad-4f4a-b41a-cad04d309e9d /home                   ext3    defaults,nodev        1 2
UUID=4b309559-2391-4f28-b2f5-38029e278db0 /tmp                    ext3    defaults        1 2
UUID=1dfc4cab-4de4-491f-8bd0-3503e9027717 /var                    xfs     defaults        0 0
UUID=f0846773-528b-42a2-a0dc-7a736595f7e1 /var/log                xfs     defaults        0 0
UUID=25ef70f1-7083-4a9b-a64c-970f5d4ad742 /var/log/audit          xfs     defaults        0 0
UUID=40d2f177-aa5c-43a1-9d85-e21bf2bafb39 swap                    swap    defaults        0 0

How can i do that using ansible and lineinfile module?

  • 3
    Why must you use lineinfile ? – Stephen Harris Nov 25 '18 at 1:35
  • I have chosen lineinfile because it was the recommended for editing file by ansible. if there is other solution am open but always using ansible. – Houssem Salhi Nov 25 '18 at 1:44
1

This can be done with lineinfile, maybe, assuming the format of the file does not change, and defaults is there to constrain the match, after which the nodev option and the rest of the line can be added:

- hosts: localhost
  tasks:
    - lineinfile:
        path: /tmp/fstab
        regexp: '^(\S+\s+/home\s+\S+\s+defaults)'
        line: '\1,nodev 1 2'
        backrefs: yes

as tested with:

$ ansible-playbook lineinfile.yml
$ diff /tmp/fstab.orig /tmp/fstab

though for mount points there is the mount module that may be less fragile than regex parsing of fstab. On the other hand the regex will not care what the UUID is, while a mount module entry may need something specific there that could vary across hosts. An additional rule or script that tests that the appropriate filesystem device is set may be necessary to catch the low but not zero odds that the regex fails.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.