Merge List of Dictionary using ansible

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

Merge List of Dictionary using ansible

问题

I am trying to merge two list of dictionaries.

"my_name":
[ {
"name": "xyz",
"number": "123" },
{
"name": "abc",
"number": "456"
}
]

"my_address":
[ {
"name": "abc",
"address": "smith street" },
{
"name": "xyz",
"number": "ray street"
}
]

Required Output:

merged:

[ {
"name": "abc",
"address": "smith street",
"number": "456" },
{
"name": "xyz",
"address": "ray street",
"number": "123"
}
]

I tried using combine but it would just combine the last item.

set_fact:
merged: "{{ my_name | combine(my_address) }}"

"merged":
[
{
"name": "xyz",
"number": "ray street",
"number": "123"
}
]

I used the debug module. debug: msg: "{{ my_name| list_mergeby(my_address,'name')}}" The error was: template error while templating string: no filter named 'lists_mergby,

英文:

I am trying to merge two list of dictionaries.

  1. "my_name":
  2. [ {
  3. "name": "xyz",
  4. "number": "123" },
  5. {
  6. "name": "abc",
  7. "number": "456"
  8. }
  9. ]
  10. "my_address":
  11. [ {
  12. "name": "abc",
  13. "address": "smith street" },
  14. {
  15. "name": "xyz",
  16. "number": "ray street"
  17. }
  18. ]

Required Output:

merged:

  1. [ {
  2. "name": "abc",
  3. "address": "smith street",
  4. "number": "456" },
  5. {
  6. "name": "xyz",
  7. "address": "ray street",
  8. "number": "123"
  9. }
  10. ]

I tried using combine but it would just combine the last item.

  1. set_fact:
  2. merged: "{{ my_name | combine(my_address) }}"
  3. "merged":
  4. [
  5. {
  6. "name": "xyz",
  7. "number": "ray street",
  8. "number": "123"
  9. }
  10. ]

i used the debug module. debug: msg: "{{ my_name| list_mergeby(my_address,'name')}}" The error was: template error while templating string: no filter named 'lists_mergby,

答案1

得分: 1

例如,给定固定的 my_address

  1. - hosts: localhost
  2. vars:
  3. my_name:
  4. - name: xyz
  5. number: '123'
  6. - name: abc
  7. number: '456'
  8. my_address:
  9. - name: abc
  10. address: smith street
  11. - name: xyz
  12. address: ray street
  13. merged: "{{ [my_name, my_address]|community.general.lists_mergeby('name') }}"
  14. tasks:
  15. - debug:
  16. var: merged

生成 (缩写)

  1. merged:
  2. - address: smith street
  3. name: abc
  4. number: '456'
  5. - address: ray street
  6. name: xyz
  7. number: '123'

请参见:过滤器 community.general.lists_mergeby

英文:

For example, given the fixed my_address

  1. - hosts: localhost
  2. vars:
  3. my_name:
  4. - name: xyz
  5. number: '123'
  6. - name: abc
  7. number: '456'
  8. my_address:
  9. - name: abc
  10. address: smith street
  11. - name: xyz
  12. address: ray street
  13. merged: "{{ [my_name, my_address]|community.general.lists_mergeby('name') }}"
  14. tasks:
  15. - debug:
  16. var: merged

gives (abridged)

  1. merged:
  2. - address: smith street
  3. name: abc
  4. number: '456'
  5. - address: ray street
  6. name: xyz
  7. number: '123'

See: the filter community.general.lists_mergeby

huangapple
  • 本文由 发表于 2023年2月19日 12:33:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498016.html
匿名

发表评论

匿名网友

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

确定