【问题标题】:Ansible : How to split json data with some seperator in json_queryAnsible:如何在 json_query 中使用一些分隔符拆分 json 数据
【发布时间】:2026-01-22 04:10:01
【问题描述】:

我正在制作一些可靠的剧本来输出 json 数据。

我通过 uri 模块获得了 json 数据。 我将数据保存为变量,甚至设法使用 json_query 提取所需的数组。

---
- name: get lti id
  hosts: web
  become: yes

  tasks:
  - name : "get lti"
    
    ...

  - name : "print returned json"
    debug:
            var: data.json

  - name : "write var to file"
    copy:
            content: " {{ data.json | json_query('[*].{id:id, url:url}') | to_nice_json }}"
            dest: "/data/test.json"

我的结果 json 文件低于一个。


 [
    {
        "id": 7,
        "url": "https://template.com/test/lti/admin/policy/red"
    },
    {
        "id": 8,
        "url": "https://template.com/test/lti/admin/blue"
    },
    {
        "id": 10,
        "url": "https://template.com/test/lti/yellow"
    }
]


但我想在此处仅提取网址的最后一部分。例如)红色,蓝色

所以我试着像这样改变我的剧本

---
- name: get lti id
  hosts: web
  become: yes

  tasks:

    ...

  - name : "print returned json"
    debug:
            var: data.json

  - name : "write var to file"
    copy:
            content: " {{ data.json | json_query('[*].{id:id, url:url}')| url.split('/')[-1] | to_nice_json }}"
            dest: "/data/test.json"

但这会导致致命错误

TASK [将变量写入文件] **************************************** ****************************************************** ************** 致命:[lms-web-template]:失败! => {“msg”:“模板字符串时模板错误:预期标记'打印语句结束',得到'['。字符串:{{ data.json | json_query('[*].{id:id, url: url}') | url.split('/')[-1] | to_nice_json }}"}

我想得到如下结果。


 [
    {
        "id": 7,
        "url": "red"
    },
    {
        "id": 8,
        "url": "blue"
    },
    {
        "id": 10,
        "url": "yellow"
    }
]

我需要你的帮助...谢谢:)

【问题讨论】:

    标签: json ansible json-query


    【解决方案1】:

    在独立任务中修改列表。例如

        - set_fact:
            content2: "{{ content2|default([]) + [item|combine({'url':url})] }}" 
          loop: "{{ content }}"
          vars:
            url: "{{ item.url.split('/')|last }}"
        - copy:
            content: "{{ content2|to_nice_json }}"
            dest: data.json
    

    给予

    shell> cat data.json 
    [
        {
            "id": "7,",
            "url": "red"
        },
        {
            "id": "8,",
            "url": "blue"
        },
        {
            "id": "10,",
            "url": "yellow"
        }
    ]
    

    下一个选项是修改 Jinja 中的数据。例如

        - copy:
            content: |
              {% for item in content %}
              - id: {{ item.id }}
                url: {{ item.url.split('/')|last }}
              {% endfor %}
            dest: data.yml
        - debug:
            msg: "{{ lookup('file', 'data.yml')|from_yaml|to_nice_json }}"
    

    给予

        [
            {
                "id": "7,",
                "url": "red"
            },
            {
                "id": "8,",
                "url": "blue"
            },
            {
                "id": "10,",
                "url": "yellow"
            }
        ]
    

    【讨论】: