4

I need to mount a smb share to have access on large, shared installation files in Ansible. This works using CLI:

- name: Mount share
  become: yes
  shell: "mount.cifs {{ smb_share.path }} {{ smb_share.mount_point }} -o user={{ smb_share.user }},password={{ smb_share.password }},mfsymlinks,exec"

However, this has two disadvantages:

  • Doesn't follow best practices of using modules instead of shell commands when required
  • No detection if already mounted - I'd need to implement this, e.g. by grep for the mountpoint in mount

There is a mount module in Ansible. But since this share ist just for installation and uses credentials, I don't want to have it permanently mounted. The boot parameter looks what I need, sadly not for Linux:

Determines if the filesystem should be mounted on boot.
Only applies to Solaris systems.

I still tried to set boot: no but as described in the docs, it still creates an /etc/fstab entry with the plain text password.

Is there any alternative to have a Windows share temprorarily mounted on CentOS 7 with any Ansible module?

Lion
  • 16,606
  • 23
  • 86
  • 148

1 Answers1

6

I am not aware there exist some temp mount specific module in ansible. But from docs you can use mount module in the next way:

- name: Mount network share
  mount:
    src: //path/to/windows/share
    path: /mnt
    fstype: cifs
    opts: 'username=example@domain,password=Password1!'
    state: mounted
  become: true

- name: Unmount network share
  mount:
    path: /mnt
    state: absent
  become: true

First task state=mounted will create record in /etc/fstab and mount that network share, and second task state=absent you can use to umount mounted share and remove corresponding record from /etc/fstab. That is the best option that comes to my mind.

bkalcho
  • 309
  • 2
  • 11