Skip to content

Ansible: Just mount an existing filesystem

There is no easy way to "just mount" an existing filesystem in Ansible, using the "mount" module. I just want to mount an already defined mount point, and don't really care about all the configuration here. But if I try this:

- name: Mount /backup filesystem
    path: "/backup"
    state: mounted

I end up with the following error message:

TASK [Mount /backup filesystem] ********************
fatal: []: FAILED! => {"changed": false, "msg": "state is mounted but all of the following are missing: src, fstype"}

Ansible requires to configure the mount point all the way. Also specified in the documentation:

  • "fstype" is required when state is "present" or "mounted"
  • "state" == "mounted": the device will be actively mounted and appropriately configured in fstab. If the mount point is not present, the mount point will be created.

Not what I want here!


My fallback is the Linux "mount" command. Also using the -v (verbose) flag, so I can figure out if the filesystem was already mounted, or if the task changed this.

- name: Mount backup device
    cmd: mount -v /backup
    warn: no
  register: mount_backup
  failed_when: mount_backup.rc != 0 and mount_backup.rc != 32
  changed_when: mount_backup.stdout.find('mounted on') != -1

"mount" will return "0" when the filesystem can be mounted, and "32" when it was already mounted.

Also STDOUT will include "mounted on" when the mount changes. (Note: you might want to ensure that your locale settings are "English").

The "warn=no" is there so Ansible will not complain about using the mount command instead of the module.

Finally this allows one to only execute subsequent commands when the mount was successful:

- block:
  - debug: msg="Mount successful"
  when: mount_backup.stdout.find('mounted on') != -1 or mount_backup.stderr.find('already mounted on') != -1

I'm using the output from STDOUT and STDERR here, because - according to the manpage - RC=32 can be anything. If the text includes "mounted on" or "already mounted on", the mount point worked.


No Trackbacks


Display comments as Linear | Threaded

No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.
Form options