DEV Community

Thomas H Jones II
Thomas H Jones II

Posted on • Originally published at thjones2.blogspot.com on

Ansible Journey: Adding /etc/fstab Entries

As noted in yesterday's post, I'm working on a new customer-project. One of the automation-tools this customer uses is Ansible. This is a new-to-me automation-technology. Previously — and aside from just writing bare BASH and Python code — I've used frameworks like Puppet, SaltStack and a couple others. So, picking up a new automation-technology — especially one that uses a DSL not terribly unlike one I was already familiar with, hasn't been super much of a stretch.

After sorting out yesterday's problem and how I wanted my /etc/fstab to look, I set about implementing it via Ansible. Ultimately, I ended up settling on a list-of-maps variable to drive a lineinfile role-task. I chose a list-of-maps variable mostly because the YAML that Ansible relies on doesn't really do tuples. My var ended up looking like:

s3fs_fstab_nest:
  - mountpoint: /provisioning/repo
    bucket: s3fs-build-bukkit
    folder: RPMs
  - mountpoint: /provisioning/installers
    bucket: s3fs-build-bukkit
    folder: EXEs
  - mountpoint: /Data/personal
    bucket: s3fs-users-bukkit
    folder: build

And my play ended up looking like:

---
- name: "Add mount to /etc/fstab"
  lineinfile:
    path: '/etc/fstab'
    line: "s3fs#{{ item.bucket }}:/{{ item.folder }}\t{{ item.mountpoint }}fuse\t_netdev,allow_other,umask=0000,nonempty 0 0"
  loop: "{{ s3fs\_fstab\_nest }}"
...

Was actually a lot simpler than I was expecting it to be.

Top comments (1)

Collapse
 
keyboardinterrupt profile image
KeyboardInterrupt • Edited

Nice Read!

If you don't like to use the lineinfile module, the mount module also does come with the ability to just create an /etc/fstab entry, without forcing a mount right away, by setting the state to present which translates to:

present only specifies that the device is to be configured in fstab and does not trigger or require a mount.

docs.ansible.com/ansible/latest/mo...

I personally avoid the use of lineinfile, blockinfile, command, shell or similar modules in favor of more fit, purpose built, modules.

Also it is Easy to mess those up, and hard to make them truly idempotent.