简体   繁体   English

Ansible 循环通过 json 输出并添加索引

[英]Ansible loop through json output and add indexing

I've been trying to figure out for hours how to do the following with Ansible, and looping through a JSON output.几个小时以来,我一直试图弄清楚如何使用 Ansible 执行以下操作,并循环遍历 JSON 输出。

Let me explain, here is my playbook:让我解释一下,这是我的剧本:

- hosts: myhosts
  connection: local
  gather_facts: yes
  tasks:
    - name: set_fact 
      set_fact: 
        disk_info: "{{ disks | json_query('[*].{disk_size: disksize}')}}"

    - name: print set_fact
      debug:
        msg: "{{ disk_info }}"

    - name: iterate on disk_info one and increase index 1..n
      debug:
        msg: "{{ item }} {{ my_idx }}"
      loop:
        - "{{ disk_info }}"
      loop_control:
        index_var: my_idx

Here is the json output that task - name: print outputs:这是任务的 json 输出- name: print输出:

TASK [print] ****************************************************
ok: [hostname1] => {
    "msg": [
        {
            "disk_size": "200"
        },
        {
            "disk_size": "200"
        },
        {
            "disk_size": "200"
        }
    ]
}
ok: [hostname2] => {
    "msg": [
        {
            "disk_size": “300"
        }
    ]
}
ok: [hostname3] => {
    "msg": [
        {
            "disk_size": "250”
        }
    ]
}
ok: [hostname4] => {
    "msg": [
        {
            "disk_size": “500"
        },
        {
            "disk_size": “600”
        }
    ]
}

Here the output I'm getting from my task ( - name: iterate on disk_info one and increase index 1..n )这里是我从我的任务中得到的输出( - name: iterate on disk_info one and increase index 1..n

ok: [hostname1] => (item=[{'disk_size': '200'}, {'disk_size': '200'}, {'disk_size': '200'}]) => {
    "msg": "[{'disk_size': '200'}, {'disk_size': '200'}, {'disk_size': '200'}] 0"
}
ok: [hostname2] => (item=[{'disk_size': ‘300'}]) => {
    "msg": "[{'disk_size': ‘300'}] 0"
}
ok: [hostname4] => (item=[{'disk_size': ‘500'}, {'disk_size': '600’}]) => {
    "msg": "[{'disk_size': ’500'}, {'disk_size': '600’}] 0"
}

Expected output:预期输出:

Hostname1:
disk_size1: 200
disk_size2: 200
disk_size3: 200

Hostname2:
disk_size1: 300

Hostname3:
disk_size1: 500
disk_size2: 600

The expected output can be something similar but adding the index.预期的输出可能是类似的,但添加了索引。

Are you trying to add the index to the structure, or to textual output?您是否尝试将索引添加到结构或文本输出中? To get your expected output, your last task should loop over the list (instead of a list that contains the list) and output the data in that form:要获得预期的输出,您的最后一个任务应该遍历列表(而不是包含列表的列表)并以该形式输出数据:

    - name: iterate on disk_info one and increase index 1..n
      debug:
        msg: "disk_size{{ my_idx }}: {{ item.disk_size }}"
      loop: "{{ disk_info }}"
      loop_control:
        index_var: my_idx

Creating a structure is more difficult, but not impossible.创建结构更困难,但并非不可能。 I'm going to demonstrate without using json_query or set_fact , which should be avoided unless you're doing something that's impossible without them.我将在不使用json_queryset_fact情况下进行演示,除非您正在做一些没有它们就不可能的事情,否则应该避免使用。

- hosts: my_hosts
  vars:
    # You didn't provide an example of your input structure, so I'm assuming it
    # looks something like this.
    disks:
      - name: foo
        disksize: 200
      - name: bar
        disksize: 200
    disk_info: "{{ dict(range(0, disks | length) | map('regex_replace', '^', 'disk_size') | zip(disks | map(attribute='disksize'))) }}"
  tasks:
    - debug:
        msg: "{{ disk_info }}"

Result:结果:

TASK [debug] *******************************************************************
ok: [harmless-ghoul.syslog.x.mail.umich.edu] => {
    "msg": {
        "disk_size0": 200,
        "disk_size1": 200
    }
}

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

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