Merge List of Dictionary using ansible

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

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.

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

答案1

得分: 1

例如,给定固定的 my_address

- hosts: localhost

  vars:

    my_name:
      - name: xyz
        number: '123'
      - name: abc
        number: '456'

    my_address:
      - name: abc
        address: smith street
      - name: xyz
        address: ray street

    merged: "{{ [my_name, my_address]|community.general.lists_mergeby('name') }}"

  tasks:

    - debug:
        var: merged

生成 (缩写)

  merged:
  - address: smith street
    name: abc
    number: '456'
  - address: ray street
    name: xyz
    number: '123'

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

英文:

For example, given the fixed my_address

- hosts: localhost

  vars:

    my_name:
      - name: xyz
        number: '123'
      - name: abc
        number: '456'

    my_address:
      - name: abc
        address: smith street
      - name: xyz
        address: ray street

    merged: "{{ [my_name, my_address]|community.general.lists_mergeby('name') }}"

  tasks:

    - debug:
        var: merged

gives (abridged)

  merged:
  - address: smith street
    name: abc
    number: '456'
  - address: ray street
    name: xyz
    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:

确定