在循环中迭代时,将向json_query值添加一个右方括号。
我正在尝试形成一条绝对路径(例如:/path/to/dir 1/data),但由于在每个返回值(从上一个任务注册的路径)的末尾添加了']',因此scp无法工作。并且输出显示返回列表之外的数据。
这里有几行输出
failed: [node1] (item={'source_path': ['/path/to/dir1/data/snapshot.20230414003127854', '/path/to/dir2/data/snapshot.20230414003128467', '/path/to/dir3/data/snapshot.20230414003129100', '/path/to/dir4/data/snapshot.20230414003129744'], 'target_path': ['/path/to/dir1', '/path/to/dir2', '/path/to/dir3', '/path/to/dir4']}) => {
"ansible_loop_var": "item",
"changed": true,
"cmd": "scp -r -o StrictHostKeyChecking=no ['/path/to/dir1/data/snapshot.20230414003127854', '/path/to/dir2/data/snapshot.20230414003128467', '/path/to/dir3/data/snapshot.20230414003129100', '/path/to/dir4/data/snapshot.20230414003129744'] user@target_node:['/path/to/dir1', '/path/to/dir2', '/path/to/dir3', '/path/to/dir4']/data",
"delta": "0:00:00.011310",
"end": "2023-04-13 19:31:43.326500",
"invocation": {
"module_args": {
"_raw_params": "scp -r -o StrictHostKeyChecking=no ['/path/to/dir1/data/snapshot.20230414003127854', '/path/to/dir2/data/snapshot.20230414003128467', '/path/to/dir3/data/snapshot.20230414003129100', '/path/to/dir4/data/snapshot.20230414003129744'] user@target_node:['/path/to/dir1', '/path/to/dir2', '/path/to/dir3', '/path/to/dir4']/data",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
"item": {
"source_path": [
"/path/to/dir1/data/snapshot.20230414003127854",
"/path/to/dir2//data/snapshot.20230414003128467",
"/path/to/dir3/data/snapshot.20230414003129100",
"/path/to/dir4/data/snapshot.20230414003129744"
],
"target_path": [
"/path/to/dir1",
"/path/to/dir2",
"/path/to/dir3",
"/path/to/dir4"
]
},
"msg": "non-zero return code",
"rc": 1,
"start": "2023-04-13 19:31:43.315190",
"stderr": "/path/to/dir1]/data: No such file or directory",
"stderr_lines": [
"/path/to/dir1]/data: No such file or directory"
],
"stdout": "",
"stdout_lines": []
}
下面是我正在使用的模块行
- name: copy files
shell: "scp -r -o StrictHostKeyChecking=no {{ item.source_path }} user@{{ target_primary_node }}:{{ item.target_path }}/data"
with_items:
- { source_path: "{{ source_path.results|json_query('[].files[].path') }}", target_path: "{{ dest_path.results|json_query('[].files[].path') }}" }
1条答案
按热度按时间4c8rllxm1#
下面的任务应该可以完成这项工作
完整的测试剧本示例