I’m trying to create a playbook that I can run whenever I spin up a new machine and one of the tasks I want to do is add a fstab entry to my NFS share where I keep all of my software installers that cannot be acquired via a package manager. What is the best route for accomplishing something like this?
As long as the line won’t change much (if ever) I’d probably use lineinfile
ansible.builtin.lineinfile – Manage lines in text files — Ansible Documentation
If it does change or you make a mistake you will have to manually reset the file, probably a good practice to back it up before change.
You can use lineinfile
but the mount
module is built specifically for this.
https://docs.ansible.com/ansible/latest/collections/ansible/posix/mount_module.html
TIL. Didn’t know about mount module
I’ve known about it for less than 24 hours.
If i understand the question you have a script you want to run to re stand up you System. You can use a simple >> to inject into fstab what ever you want.
I do it all the time example -
echo ‘UUID=5847-C38A /boot/efi vfat defaults 0 1’ >> /etc/fstab
you do need su rights to do this since fstab is protected.
You’re answering the question: “how do I add an fstab entry with a shell script” which is not what’s being asked.
OP wants to know how to do it in an ansible playbook.
Yes I know … I am giving him a starter place, it’s the same just how you call it is different. An example roughly below
ansible.builtin.shell: echo ‘UUID=5847-C38A /boot/efi vfat defaults 0 1’ >> fstab
args:
executable: /etc
And here if he wants to make multiple calls in one it’s the same …
ansible.builtin.shell: |
echo ‘UUID=5847-C38A /boot/efi vfat defaults 0 1’ >> fstab
echo ‘UUID=XXXX-XXXX /other1 vfat defaults 0 1’ >> fstab
echo ‘UUID=YYYY-YYYY /other2 vfat defaults 0 1’ >> fstab
become: true
args:
chdir: /etc
These are all rough examples that need to be trailered to his needs and won’t work by copy paste since there is not enough info from the question … Hence the simple original post it’s the same no matter what
It’s kind of missing the point though. You’d only want to issue shell commands with ansible when there is no suitable module to perform the task. None of your examples are idempotent either, unlike mount
or lineinfile
.