连接到共享文件夹 .14.2.130\shared\ 后,我试图在共享文件夹内的“数据”文件夹中搜索文件。
- name: Search for files in Data folder
win_find:
paths: \\10.14.2.130\shared\folder\Data\
register: file1
任务应该显示文件夹中有一个文件。 然而,这是我得到的错误:
**[WARNING]: Argument path '\\10.14.2.130\shared\folder\Data\' does not exist or is not accessible, skipping**
ok: [10.12.201.60] => {
"changed": false,
"examined": 0,
"files": [],
"invocation": {
"module_args": {
"age": null,
"age_stamp": "mtime",
"checksum_algorithm": "sha1",
"file_type": "file",
"follow": false,
"get_checksum": true,
"hidden": false,
"paths": [
"\\\\10.14.2.130\\shared\\folder\\Data\\"
],
"patterns": null,
"recurse": false,
"size": null,
"use_regex": false
}
},
"matched": 0
}
但是,当我在共享文件夹主机本身(10.14.2.130)上运行相同的任务时,它设法输出文件夹中有一个文件的结果:
ok: [10.14.2.130] => {
"changed": false,
"examined": 1,
"files": [
{
"attributes": "Archive",
"checksum": "141a1f8872be45d532973c56688cf800700b01bb",
"creationtime": 1678294890.4031632,
"exists": true,
"extension": ".csv",
"filename": "sample.csv",
"hlnk_targets": [],
"isarchive": true,
"isdir": false,
"ishidden": false,
"isjunction": false,
"islnk": false,
"isreadonly": false,
"isreg": true,
"isshared": false,
"lastaccesstime": 1678294890.4031632,
"lastwritetime": 1677091528,
"lnk_source": null,
"lnk_target": null,
"nlink": 1,
"owner": "BUILTIN\\Administrators",
"path": "\\\\10.14.2.130\\shared\\folder\\Data\\sample.csv",
"sharename": null,
"size": 949
}
],
"invocation": {
"module_args": {
"age": null,
"age_stamp": "mtime",
"checksum_algorithm": "sha1",
"file_type": "file",
"follow": false,
"get_checksum": true,
"hidden": false,
"paths": [
"\\\\10.14.2.130\\shared\\folder\\Data\\"
],
"patterns": null,
"recurse": false,
"size": null,
"use_regex": false
}
},
"matched": 1
编辑:我使用此任务连接到共享文件夹:
- name: Connect to shared folder
win_command: 'net use Z: \\10.14.2.130\shared\ /user:username passwordhere /p:yes'
become: yes
become_method: runas
become_user: SYSTEM
我已经检查过,发现它在文件资源管理器中显示为断开连接的驱动器,但我仍然可以单击它来查看其中的文件。 是ansible找不到共享文件夹路径是因为驱动断开了吗?我如何使用 Ansible 成功连接到驱动器,以便它可以搜索共享文件夹中的文件?
我在 win_find 任务中输入 become、become_method 和 become_user 后任务能够产生正确的输出:
- name: Search for files in Data folder
win_find:
paths: \\10.14.2.130\shared\folder\Data\
register: file1
become: yes
become_method: runas
become_user: SYSTEM