gpt4 book ai didi

powershell - Ansible Playbook - 从 Windows 远程机器获取文件

转载 作者:行者123 更新时间:2023-12-02 22:41:44 24 4
gpt4 key购买 nike

我有一个剧本可以做 3 件事 -

  1. 查找主机中的目录列表
  2. 根据找到的目录找到文件列表
  3. 将文件提取到我的本地服务器。

我使用 win_shell 模块来查找目录和文件,调试结果显示它可以正确找到它们。但是,当我使用 fetch 模块时,它指出我注册到循环中的文件是目录 -

---

- hosts: Host
gather_facts: no
tasks:

- name: Find DIR Directories
win_shell: Get-ChildItem {{ partition }}:\Main\Production\App\ -Recurse -Directory "DIR" -Exclude "NotInUse" |% {Write-Host $_.FullName}
register: dataDirs
- debug:
var: dataDirs

- name: Find Each DIR Files
win_shell: Get-ChildItem {{ item }} *.csv -Recurse |% {Write-Host $_.FullName}
with_items: "{{ dataDirs.stdout_lines }}"
register: dataFiles
- debug:
var: dataFiles

- name: Fetch All Files
fetch: src= "{{ item.stdout_lines }}" dest=/local/DIRFiles/
with_items: "{{ dataFiles.results }}"
...

结果是——

PLAY [Host] *******************************************************************************************************************************************************************************************

TASK [Find DIR Directories] *********************************************************************************************************************************************************************
changed: [Host]

TASK [debug] *******************************************************************************************************************************************************************************************
ok: [Host] => {
"dataDirs": {
"changed": true,
"cmd": "Get-ChildItem d:\\Main\\Production\\APP\\ -Recurse -Directory \"DIR\" -Exclude \"NotInUse\" |% {Write-Host $_.FullName}",
"delta": "0:00:00.643620",
"end": "2020-10-07 09:39:50.528396",
"failed": false,
"rc": 0,
"start": "2020-10-07 09:39:49.884775",
"stderr": "",
"stderr_lines": [],
"stdout": "d:\\DIR\\Production\\APP\\DIR\n",
"stdout_lines": [
"d:\\DIR\\Production\\APP\\DIR"
]
}
}

TASK [Find Each DataForPnL Files] **********************************************************************************************************************************************************************
changed: [Host] => (item=d:\\DIR\\Production\\APP\\DIR)

TASK [debug] *******************************************************************************************************************************************************************************************
ok: [Host] => {
"dataFiles": {
"changed": true,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "Get-ChildItem d:\\Main\\Production\\APP\\DIR *.csv -Recurse |% {Write-Host $_.FullName}",
"delta": "0:00:00.555535",
"end": "2020-10-07 09:39:53.259030",
"failed": false,
"item": "d:\\DIR\\Production\\APP\\DIR",
"rc": 0,
"start": "2020-10-07 09:39:52.703494",
"stderr": "",
"stderr_lines": [],
"stdout": "d:\\Main\\Production\\APP\\DIR\\PnlVol\\EURIBOR.csv\nD:d:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-10-08 SettleFix.csv\nd:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-10-28 SettleFix.csv\nd:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-12-24 SettleFix.csv\n",
"stdout_lines": [
"d:\\Main\\Production\\APP\\DIR\\PnlVol\\EURIBOR.csv",
"d:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-10-08 SettleFix.csv",
"d:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-10-28 SettleFix.csv",
"d:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-12-24 SettleFix.csv"
]
}
]
}
}

TASK [Fetch All Files] *********************************************************************************************************************************************************************************
failed: [Host] (item={'start': '2020-10-07 09:39:52.703494', 'stdout': 'd:\\Main\\Production\\APP\\DIR\\PnlVol\\EURIBOR.csv\nD:d:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-10-08 SettleFix.csv\nd:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-10-28 SettleFix.csv\nd:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-12-24 SettleFix.csv\n', 'cmd': 'Get-ChildItem d:\\Main\\Production\\APP\\DIR *.csv -Recurse |% {Write-Host $_.FullName}', 'stderr': '', 'changed': True, 'rc': 0, 'delta': '0:00:00.555535', 'end': '2020-10-07 09:39:53.259030', 'stdout_lines': ['d:\\Main\\Production\\APP\\DIR\\PnlVol\\EURIBOR.csv\nD:d:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-10-08 SettleFix.csv\nd:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-10-28 SettleFix.csv\nd:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-12-24 SettleFix.csv\n'], 'stderr_lines': [], 'failed': False, 'item': 'd:\\Main\\Production\\APP\\DIR', 'ansible_loop_var': 'item'}) => {
"ansible_loop_var": "item",
"changed": false,
"file": ".",
"item": {
"ansible_loop_var": "item",
"changed": true,
"cmd": "Get-ChildItem d:\\Main\\Production\\APP\\DIR *.csv -Recurse |% {Write-Host $_.FullName}",
"delta": "0:00:00.555535",
"end": "2020-10-07 09:39:53.259030",
"failed": false,
"item": "d:\\DIR\\Production\\APP\\DIR",
"rc": 0,
"start": "2020-10-07 09:39:52.703494",
"stderr": "",
"stderr_lines": [],
"stdout": "d:\\Main\\Production\\APP\\DIR\\PnlVol\\EURIBOR.csv\nD:d:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-10-08 SettleFix.csv\nd:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-10-28 SettleFix.csv\nd:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-12-24 SettleFix.csv\n",
"stdout_lines": [
d:\\DIR\\Production\\APP\\DIR\\PnlVol\\EURIBOR.csv",
"d:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-10-08 SettleFix.csv",
"d:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-10-28 SettleFix.csv",
"d:\\Main\\Production\\APP\\DIR\\SettleFix\\2019-12-24 SettleFix.csv"
]
}
}

MSG:

**remote file is a directory, fetch cannot work on directories**

PLAY RECAP *********************************************************************************************************************************************************************************************
Host : ok=4 changed=2 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

我厌倦了将第二个 win_shell 更改为 win_find,它一直告诉我这些文件是目录。

我做错了什么?谢谢。

最佳答案

fetch documentation说明 src parameterstring 类型的值,表示单个文件的路径。您正在传递一个列表

您要做的是在每个 results 中遍历 stdout_lines 中的每个文件。

实现此目的的一种方法:

  - name: Fetch All Files
fetch:
src: "{{ item.1 }}"
dest: /local/DIRFiles/
with_subelements:
- "{{ dataFiles.results }}"
- stdout_lines

引用:

关于powershell - Ansible Playbook - 从 Windows 远程机器获取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64241598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com