Bootstrap FreeKB - Ansible - Mount and unmount volumes, shares, and partitions
Ansible - Mount and unmount volumes, shares, and partitions

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

The mount module is used to:

Sometimes, the mount module is used in conjunction with the ansible_mounts fact. The ansible_mounts fact will contain each entry in the target servers /etc/fstab file.

Here is how you could umount a volume, share or partition.

---
- hosts: all
  tasks:
  - name: umount /mnt/my_mount
    mount:
      path: /mnt/my_mount
      state: absent
...

 

And here is how you can use both the mount module and the ansible_mounts fact to unmount NFS shares.

---
- hosts: all
  tasks:
  - name: umount NFS shares
    mount:
      path: "{{ item.mount }}"
      state: absent
    with_items: "{{ ansible_mounts }}"
    when: item.fstype is search 'nfs'
    loop_control:
      label: "{{ item.mount }}"
...

 

Let's say /etc/fstab on one of your managed nodes contains the follow.

UUID=7a8e7883-e6ee-4482-a7ca-665bfe94eaa8 /var  ext4  defaults    1 2

 

absent can be used to remove entry from /etc/fstab. Since the /etc/fstab file is probably owned by root, the playbook will need to be run as remote_user root.

---
- hosts: all
  remote_user: root
  tasks:
  - name: remove /var entry from /etc/fstab
    mount:
      path: /var
      state: absent
...

 

Or using become.

---
- hosts: all
  remote_user: john.doe
  tasks:
  - name: remove /var entry from /etc/fstab
    become: yes
    become_user: root
    mount:
      path: /var
      state: absent
...

 

Let's say fatal error target is busy or device is busy is returned when attempting to unmount.

fatal: [server1.example.com]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"}, "changed": false, "msg": "Error unmounting /var: umount: /var: target is busy.\n"}

 

The Ansible mount module does not have an option to unmount with the --lazy flag. Sometimes, the --lazy flag will successfully unmount a device that returned target is busy or device is busy. Since the Ansible mount module does not have an option to unmount with the --lazy flag, command or shell can be used to issue the unmount command with the --lazy flag.

---
- hosts: all
  remote_user: root
  tasks:
  - name: unmount /var
    shell: "umount --lazy /var"
...

 

There is no option to mount the volumes/partitions/shares in the /etc/fstab file, so you can use command or shell to invoke the mount --all command command. Almost always, I would first use systemd to reload daemons in case a change was made to the /etc/fstab file which requires the daemons to be reloaded.

---
- hosts: all
  remote_user: root
  tasks:
  - name: reload systemd daemons
    systemd:
      daemon_reload: yes

  - name: mount the volumes/partitions/shares in /etc/fstab
    shell: "mount --all"
...

 

And here is how you would add entry back to /etc/fstab. By using state: mounted, and entry will be created or updated in the /etc/fstab file and the device will be mounted (like using the mount command).

---
- hosts: all
  remote_user: root
  tasks:
  - name: add /var entry to /etc/fstab
    mount:
      path: /var
      src: UUID=7a8e7883-e6ee-4482-a7ca-665bfe94eaa8
      fstype: ext4
      opts: defaults
      dump: '1'
      state: mounted
...

 

You probably do not want to hard code in the UUID of each device into your playbook. Instead, the gather_facts plugin can be used to get the UUID.

---
- hosts: all
  remote_user: root
  tasks:
    - name: add /var entry to /etc/fstab
      mount:
        path: /var
        src: UUID="{{ ansible_devices.sda.partitions.sda1.uuid }}"
        fstype: ext4
        opts: defaults
        dump: '1'
        state: mounted
    when: ansible_devices.sda.partitions.sda1 is defined
...

 

Here is an example of how you could mount a CIFS share.

---
- hosts: all
  remote_user: root
  tasks:
  - name: mount //server1.example.com/share to /usr/local/flask/db
    mount:
      path: /usr/local/my/share
      src: //server1.example.com/share
      fstype: cifs
      opts: username=john.doe,password=itsasecret
      dump: '1'
      state: mounted
...

 

And here is an example of how to add an entry to /etc/fstab to mount an Amazon Web Services (AWS) S3 Bucket.

---
- name: main play
  hosts:all
  remote_user: john.doe
  gather_facts: false
  tasks:
  - name: add an entry to /etc/fstab so that the bucket is mounted when the VM is restarted
    become: yes
    become_user: root
    mount:
      path: /usr/local/aws/my-bucket-abcdefg
      src: s3fs#my-bucket-abcdefg
      fstype: fuse
      opts: /home/john.doe/.passwd-s3fs,allow_other
      state: present
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 99475d in the box below so that we can be sure you are a human.