-1

I am unable to use Ansible localhost (Linux) to connect to a shared folder on a Windows Server.

I am getting [Errno 2] No such file or directory: b'net'. However, I have checked that my file path to the shared folder is correct, so I am not sure how to resolve this error.

The shared folder task as written in my playbook:

- name: Connect to shared folder
  command: net use * '\\10.15.250.110\mixeddocs' /user:{{ username }} {{ password }} /p:no
  become: yes

The full error message:

The full traceback is:
  File "/tmp/ansible_ansible.legacy.command_payload_tjjro3uh/ansible_ansible.legacy.command_payload.zip/ansible/module_utils/basic.py", line 2022, in run_command
    cmd = subprocess.Popen(args, **kwargs)
  File "/usr/lib64/python3.9/subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib64/python3.9/subprocess.py", line 1821, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
fatal: [localhost]: FAILED! => {
    "changed": false,
    "cmd": "net use '*' '\\\\10.15.250.110\\mixeddocs' /user:newuser Password /p:no",
    "invocation": {
        "module_args": {
            "_raw_params": "net use * '\\\\10.15.250.110\\mixeddocs' /user:newuser Password /p:no",
            "_uses_shell": false,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true,
            "warn": false
        }
    },
    "msg": "[Errno 2] No such file or directory: b'net'",
    "rc": 2,
    "stderr": "",
    "stderr_lines": [],
    "stdout": "",
    "stdout_lines": []
}

1 Answer 1

2

The error messages says that the binary net is not found on the system. Hence, b'net'.
This is an expected output because the net command is not available on Linux, it is a Windows command.

How to mount a Windows share in Linux manually you can find in this article:
How to mount Windows share on Red Hat Enterprise Linux system using CIFS?

Since you want to do it in Ansible on the localhost by judging your output, you can do something like this:

mount_samba_localhost.yml:

- name: Setup SMB share
  hosts: localhost
  become: yes
  vars:
    username: smb_username
    password: smb_password
    domain: domain
    share_path: '//10.15.250.110/mixeddocs'
    mount_point: /mnt/my_mount_point
    credentials_file: /root/smb_credentials

  tasks:
    - name: Install cifs-utils
      ansible.builtin.package:
        name: cifs-utils
        state: present

    - name: Create directory for mount point
      ansible.builtin.file:
        path: "{{ mount_point }}"
        state: directory

    - name: Create credentials file
      ansible.builtin.copy:
        dest: "{{ credentials_file }}"
        content: |
          username={{ username }}
          password={{ password }}
          domain={{ domain }}
        mode: 0600
        owner: root
        group: root

    - name: Mount Windows share folder
      ansible.posix.mount:
        path: "{{ mount_point }}"
        src: "{{ share_path }}"
        fstype: cifs
        opts: 'credentials={{ credentials_file }}'
        state: mounted

This creates a credentials file in the root directory. In general, it is a best practice to avoid adding plain text passwords in the fstab file for security reasons. That's why a separate credentials file is created. Furthermore, an entry will be added to the fstab to ensure the mount persists across system reboots by ansible.posix.mount.

Though, I recommend using NFS-shares instead of CIFS.

Have fun!

5
  • Thanks for your contribution, I will try this. May I know why NFS instead of CIFS?
    – 865296
    Jul 12 at 1:52
  • Also, if I want to use NFS, do I just replace all the CIFS with NFS? Are the modules required still the same?
    – 865296
    Jul 12 at 1:53
  • The advice is actually when the majority of your environment are UNIX/Linux systems. However, if the majority of your systems are Windows please keep using CIFS. Very nice info where both are getting compared: CIFS vs NFS
    – Bombaci
    Jul 12 at 21:36
  • So I tried your solution, replaced CIFS with NFS and created the credentials file in a non-root directory. Unfortunately, it failed at the mount task. The error is: "msg": "Error mounting /mnt/my_mount_point: mount.nfs: remote share not in 'host:dir' format\n"
    – 865296
    Jul 13 at 19:11
  • NFS is a different thing than CIFS. If you want to mount NFS, you should create the NFS-Export on the server which is going to export the mount, in this case on 10.15.250.110. And then you can mount the NFS on the specific Linux Machine. Deploy NFSIf your server only has the CIFS share configured, stick with fstype: cifs. Read this to get additional information on how to mount different types with Ansible: Ansible mount_module
    – Bombaci
    Jul 14 at 14:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .