I need to set up an application directory for a "hairy" app. Depending on the case, the directory may be local to each server participating, or shared among several servers via NFS.
So, I need to be able to detect, whether the given path is local or NFS-accessed and skip some of the tasks in the latter case.
What's the best way to detect this in an Ansible role?
I tried using the stat module, but device_type
seems to be set to 0 in all cases, NFS or local (XFS).
On Linux I could invoke stat -f /path
-- this would output details, including the type (the utility uses the statfs
syscall). But this is a Linux-only method, and I'd rather avoid such petty OS-dependencies (same goes for the mountpoint
utility).
I would write a custom library function, but there is no os.statfs
in Python...
What's left?
Another approach would make use of Ansible facts. You could filter the ansible_mounts array for your mount=your mount point and extract the filesystem type field. For an example, please see the answer I got here: https://stackoverflow.com/a/49662135/1268949 .
Another example from my production code:
If the GNU stat utility is available on your target platforms, then you can invoke it in a way that doesn't make use of the statfs call to detect the mount-point and then search it in the output of mount, e.g. on Linux:
I verified that this invocation of stat only makes use of standard system calls (see CONFORMING TO in the man page of stat(2)):