分离字典列表

huangapple go评论96阅读模式
英文:

Separating a list of dicts

问题

以下是您要翻译的内容:

"I've been trying to find a way to take a data collection that contains three fields where two of the fields can contain multiple items and convert/expand that data to a list where a row is added for every item in one of the fields.

The data is about kubernetes pods and the containers and images inside them. I can get the raw data just fine I just can't transform it.

the json data is this:

  1. - name: condense the raw data
  2. set_fact:
  3. pod_data: "{{ pod_data|default([]) + [{'pod_name':item.pod_name,'container_name':item.container_name,'image_path':item.image_path }] }}"
  4. with_items: "{{ podinfo | json_query('resources[*].{pod_name: metadata.name, container_name: spec.containers[*].name, image_path: spec.containers[*].image }') }}"
  5. - debug: var=pod_data
  6. "pod_data": [
  7. {
  8. "container_name": [
  9. "container1",
  10. "container2"
  11. ],
  12. "image_path": [
  13. "path1",
  14. "path2"
  15. ],
  16. "pod_name": "pod1"
  17. },
  18. {
  19. "container_name": [
  20. "container3",
  21. "container4"
  22. ],
  23. "image_path": [
  24. "path3",
  25. "path4"]
  26. ],
  27. "pod_name": "pod2"
  28. }

I want to output the data in a 1:1 format like this:

  1. "pod_data": [
  2. {
  3. "container_name": "container1"
  4. ,
  5. "image_path": "path1"
  6. ,
  7. "pod_name": "pod1"
  8. },
  9. {
  10. "container_name": "container2"
  11. ,
  12. "image_path": "path2"
  13. ],
  14. "pod_name": "pod1"
  15. },
  16. {
  17. "container_name": "container3"
  18. ,
  19. "image_path": "path3"
  20. ,
  21. "pod_name": "pod2"
  22. }
  23. ```"
  24. I'm not good enough with loops to know how to do a nested style loop when there are multiple sub elements in ansible. I could do it in powershell but that doesn't help me here.
  25. Is my only choice some sort of include_tasks loop while passing each item and container/image count so I can split it that many times?"
  26. 希望这可以帮助您。如果您有任何其他疑问,请随时提出。
  27. <details>
  28. <summary>英文:</summary>
  29. I&#39;ve been trying to find a way to take a data collection that contains three fields where two of the fields can contain multiple items and convert/expand that data to a list where a row is added for every item in one of the fields.
  30. The data is about kubernetes pods and the containers and images inside them. I can get the raw data just fine I just can&#39;t transform it.
  31. the json data is this:
  • name: condense the raw data
    set_fact:
    pod_data: "{{ pod_data|default([]) + [{'pod_name':item.pod_name,'container_name':item.container_name,'image_path':item.image_path }] }}"
    with_items: "{{ podinfo | json_query('resources[].{pod_name: metadata.name, container_name: spec.containers[].name, image_path: spec.containers[*].image }') }}"

  • debug: var=pod_data

"pod_data": [
{
"container_name": [
"container1",
"container2"
],
"image_path": [
"path1",
"path2"
],
"pod_name": "pod1"
},
{
"container_name": [
"container3",
"container4"
],
"image_path": [
"path3",
"path4"
],
"pod_name": "pod2"
}

  1. I want to output the data in a 1:1 format like this:

"pod_data": [
{
"container_name": "container1"
,
"image_path": "path1"
,
"pod_name": "pod1"
},
{
"container_name": "container2"
,
"image_path": "path2"
],
"pod_name": "pod1"
},
{
"container_name": "container3"
,
"image_path": "path3"
,
"pod_name": "pod2"
}

  1. I&#39;m not good enough with loops to know how to do a nested style loop when there are multiple sub elements in ansible. I could do it in powershell but that doesn&#39;t help me here.
  2. Is my only choice some sort of include_tasks loop while passing each item and container/image count so I can split it that many times?
  3. </details>
  4. # 答案1
  5. **得分**: 3
  6. 创建结构
  7. ```yaml
  8. pod_data_update: |
  9. [{% for i in pod_data %}
  10. {% for n,p in i.container_name|zip(i.image_path) %}
  11. {container_name: {{ n }}, image_path: {{ p }}, pod_name: {{ i.pod_name }}},
  12. {% endfor %}
  13. {% endfor %}]

, 将其转换为 YAML,并更新 pod_data

  1. - set_fact:
  2. pod_data: &quot;{{ pod_data_update|from_yaml }}&quot;

获得所需结果

  1. pod_data:
  2. - {container_name: container1, image_path: path1, pod_name: pod1}
  3. - {container_name: container2, image_path: path2, pod_name: pod1}
  4. - {container_name: container3, image_path: path3, pod_name: pod2}
  5. - {container_name: container4, image_path: path4, pod_name: pod2}

<hr>

<sup>

测试的完整 playbook 示例

  1. - hosts: localhost
  2. vars:
  3. pod_data:
  4. - container_name:
  5. - container1
  6. - container2
  7. image_path:
  8. - path1
  9. - path2
  10. pod_name: pod1
  11. - container_name:
  12. - container3
  13. - container4
  14. image_path:
  15. - path3
  16. - path4
  17. pod_name: pod2
  18. pod_data_update: |
  19. [{% for i in pod_data %}
  20. {% for n,p in i.container_name|zip(i.image_path) %}
  21. {container_name: {{ n }}, image_path: {{ p }}, pod_name: {{ i.pod_name }}},
  22. {% endfor %}
  23. {% endfor %}]
  24. tasks:
  25. - set_fact:
  26. pod_data: &quot;{{ pod_data_update|from_yaml }}&quot;
  27. - debug:
  28. var: pod_data|to_yaml

</sup>

英文:

Create the structure

  1. pod_data_update: |
  2. [{% for i in pod_data %}
  3. {% for n,p in i.container_name|zip(i.image_path) %}
  4. {container_name: {{ n }}, image_path: {{ p }}, pod_name: {{ i.pod_name }}},
  5. {% endfor %}
  6. {% endfor %}]

, convert it from YAML, and update pod_data

  1. - set_fact:
  2. pod_data: &quot;{{ pod_data_update|from_yaml }}&quot;

gives what you want

  1. pod_data:
  2. - {container_name: container1, image_path: path1, pod_name: pod1}
  3. - {container_name: container2, image_path: path2, pod_name: pod1}
  4. - {container_name: container3, image_path: path3, pod_name: pod2}
  5. - {container_name: container4, image_path: path4, pod_name: pod2}

<hr>

<sup>

Example of a complete playbook for testing

  1. - hosts: localhost
  2. vars:
  3. pod_data:
  4. - container_name:
  5. - container1
  6. - container2
  7. image_path:
  8. - path1
  9. - path2
  10. pod_name: pod1
  11. - container_name:
  12. - container3
  13. - container4
  14. image_path:
  15. - path3
  16. - path4
  17. pod_name: pod2
  18. pod_data_update: |
  19. [{% for i in pod_data %}
  20. {% for n,p in i.container_name|zip(i.image_path) %}
  21. {container_name: {{ n }}, image_path: {{ p }}, pod_name: {{ i.pod_name }}},
  22. {% endfor %}
  23. {% endfor %}]
  24. tasks:
  25. - set_fact:
  26. pod_data: &quot;{{ pod_data_update|from_yaml }}&quot;
  27. - debug:
  28. var: pod_data|to_yaml

</sup>

huangapple
  • 本文由 发表于 2023年4月20日 04:42:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定