json Ansible为项目添加了结束方括号

a5g8bdjr  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(94)

在循环中迭代时,将向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') }}" }
4c8rllxm

4c8rllxm1#

下面的任务应该可以完成这项工作

- command: >-
        scp -r -o StrictHostKeyChecking=no
        {{ item.0 }}
        user@{{ target_primary_node }}:{{ item.1 }}/data
      loop: "{{ spath|zip(tpath) }}"
      vars:
        spath: "{{ source_path.results|json_query('[].files[].path') }}"
        tpath: "{{ dest_path.results|json_query('[].files[].path') }}"

完整的测试剧本示例

- hosts: localhost

  vars:

    spath:
      - /path/to/dir1/data/snapshot.20230414003127854
      - /path/to/dir2//data/snapshot.20230414003128467
      - /path/to/dir3/data/snapshot.20230414003129100
      - /path/to/dir4/data/snapshot.2023041400312974
    tpath:
      - /path/to/dir1
      - /path/to/dir2
      - /path/to/dir3
      - /path/to/dir4

  tasks:

    - debug:
        msg: >-
          scp -r -o StrictHostKeyChecking=no
          {{ item.0 }}
          user@{{ target_primary_node }}:{{ item.1 }}/data
      loop: "{{ spath|zip(tpath) }}"
      vars:
        target_primary_node: srv.example.com
        # spath: "{{ source_path.results|json_query('[].files[].path') }}"
        # tpath: "{{ dest_path.results|json_query('[].files[].path') }}"

    - command: >-
        echo
        scp -r -o StrictHostKeyChecking=no
        {{ item.0 }}
        user@{{ target_primary_node }}:{{ item.1 }}/data
      loop: "{{ spath|zip(tpath) }}"
      register: out
      vars:
        target_primary_node: srv.example.com
        # spath: "{{ source_path.results|json_query('[].files[].path') }}"
        # tpath: "{{ dest_path.results|json_query('[].files[].path') }}"
    - debug:
        msg: "{{ out.results|map(attribute='stdout')|list }}"

相关问题