Changing /etc/fstab with puppet and augeas
Puppet Augeas documentation
My case: Add tmpfs
Trying with augtool, and breaking my head on how to add an entry at the end of the file, I realized eventually figured out, that you don't have to care at all with augeas, just add a 01 prefixed value and it will be added to the end.
So to add I use:
defvar fstab /files/etc/fstab
set $fstab/01/spec tmpfs
set $fstab/01/file /dev/shm
set $fstab/01/vfstype tmpfs
set $fstab/01/opt defaults
set $fstab/01/dump 0
set $fstab/01/passno 0
save
But what if the entry already exists?
Since in fstab the entries are just numbered, it took a while to figure out how to use the last() function.
This should work (e.g. change option)
set $fstab/*[spec = 'tmpfs'][file = '/dev/shm']/opt size
set $fstab/*[spec = 'tmpfs'][file = '/dev/shm']/opt/value 12G
This will only change/add the option
size=12G, where spec equals 'tmpfs' AND file equals '/dev/shm'.
Then, all left to do was finding a rule for puppet to not add a new line on each run, that's done quite easily with 'match':
match *[spec='tmpfs'][file = '/dev/shm']
So in puppet it looks like this:
augeas{"Add tmpfs for case ${caseusername}@${hostname}":
context => "/files/etc/fstab",
changes => [
"set 01/spec tmpfs",
"set 01/file /dev/shm",
"set 01/vfstype tmpfs",
"set 01/opt size",
"set 01/opt value ${shmem}",
"set 01/dump 0",
"set 01/passno 0",
],
onlyif => "match *[spec='tmpfs'][file = '/dev/shm'] size == 0",
}
augeas{"change tmpfs size for ${caseusername}@${hostname} to ${shmem}":
context => "/files/etc/fstab",
changes => [
"set *[spec = 'tmpfs'][file = '/dev/shm']/opt size",
"set *[spec = 'tmpfs'][file = '/dev/shm']/opt/value ${shmem}",
],
require => Augeas["Add tmpfs for case ${caseusername}@${hostname}"],
}