简体   繁体   English

Ansible:如何在 json_query 中使用一些分隔符拆分 json 数据

[英]Ansible : How to split json data with some seperator in json_query

Im making some ansible playbook to output json data.我正在制作一些 ansible 剧本到 output json 数据。

I got json data through uri module.我通过 uri 模块获得了 json 数据。 I saved the data as a variable and even managed to extract the desired array with json_query.我将数据保存为变量,甚至设法使用 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"

and my result json file is below one.我的结果 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"
    }
]


But I'd like to additionally extract only the last part of the url here.但我想在这里只提取 url 的最后一部分。 ex) red, blue例如)红色,蓝色

so I tried to change my playbook like this所以我试着像这样改变我的剧本

---
- 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"

but this makes fatal error但这会导致致命错误

TASK [write var to file] ******************************************************************************************************** fatal: [lms-web-template]: FAILED: => {"msg": "template error while templating string, expected token 'end of print statement'. got '[': String. {{ data.json | json_query('[*]:{id,id: url.url}') | url.split('/')[-1] | to_nice_json }}"}任务 [将变量写入文件] ******************************************* ****************************************************** *********** 致命:[lms-web-template]: FAILED: => {"msg": "模版字符串时出现模板错误,预期标记'打印语句结束'。得到'[' :字符串。{{ data.json | json_query('[*]:{id,id: url.url}') | url.split('/')[-1] | to_nice_json }}"}

I want to get the result like below.我想得到如下结果。


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

I need your help... Thank you:)我需要你的帮助...谢谢:)

Modify the list in a standalone task.在独立任务中修改列表。 For example例如

    - 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

gives

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

The next option is the modification of the data in Jinja.下一个选项是修改 Jinja 中的数据。 For example例如

    - 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 }}"

gives

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM