尝试使用 Ansible localhost 连接到 Windows 共享文件夹时出现“[Errno 2] 没有这样的文件或目录:b'net'”

尝试使用 Ansible localhost 连接到 Windows 共享文件夹时出现“[Errno 2] 没有这样的文件或目录:b'net'”

我无法使用 Ansible localhost(Linux)连接到 Windows Server 上的共享文件夹。

我得到了[Errno 2] No such file or directory: b'net'。但是,我已检查共享文件夹的文件路径是否正确,因此我不确定如何解决此错误。

我的剧本中写的共享文件夹任务:

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

完整错误信息:

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

错误消息表明二进制 net在系统上找不到。因此,b'net'
这是预期的输出,因为该net命令在 Linux 上不可用,它是一个 Windows 命令。

您可以在本文中找到如何在 Linux 中手动安装 Windows 共享:
如何使用 CIFS 在 Red Hat Enterprise Linux 系统上挂载 Windows 共享?

由于您想通过判断输出在本地主机上的 Ansible 中执行此操作,因此您可以执行以下操作:

安装_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

这会在根目录中创建一个凭证文件。通常,出于安全原因,最好避免在文件中添加纯文本密码fstab。这就是创建单独凭证文件的原因。此外,还会将条目添加到fstab以确保挂载在系统重启后仍然存在ansible.posix.mount

不过,我建议使用NFS-shares而不是CIFS

玩得开心!

相关内容