Ansible - 列表帮助 | 2 个列表 | 1 个列表之间的唯一键值
Ansible - List assistance | 2 lists | 1 unique key value between the lists
第一次发帖,轻点。
寻求有关如何完成以下任务的帮助。
2 个列表
1 个在列表
之间共享 key/value
寻找如何根据两个列表中存在的一个 1 共享 key/value 将 list1 的所有元素/(key/value 元素?)关联到 list2
使用:usecase_key4 因为他们共享密钥 / usecase_valuse4-A / B 对于示例数据,值会更改但对于关联所需的数据集将相同。
感谢任何反馈
根据回复修改。
我尝试了 [ selectattr 和 json_query ] 的一些变体
不确定这是否是正确的方法。
"list-1": [
{
"list1_key1": "list1_value1-A"
"list1_key2": "list1_value2-A"
"list1_key3": "list1_value3-A"
"usecase_key4": "usecase_value4-A"
"list1_key5": "list1_value5-A"
"list1_key6": "list1_value6-A"
"list1_key7": "list1_value7-A"
"list1_key8": "list1_value8-A"
"list1_key9": "list1_value9-A"
"list1_key10":"list1_value10-A"
},
{
"list1_key1": "list1_value1-B"
"list1_key2": "list1_value2-B"
"list1_key3": "list1_value3-B"
"usecase_key4": "usecase_value4-B"
"list1_key5": "list1_value5-B"
"list1_key6": "list1_value6-B"
"list1_key7": "list1_value7-B"
"list1_key8": "list1_value8-B"
"list1_key9": "list1_value9-B"
"list1_key10": "list1_value10-B"
}
]
"list-2": [
{
"list2_key1": "list2_value1-A"
"list2_key2": "list2_value2-A"
"list2_key3": "list2_value3-A"
"usecase_key4": "usecase_value4-A"
"list2_key5": "list2_value5-A"
"list2_key6": "list2_value6-A"
},
{
"list2_key1": "list2_value1-B"
"list2_key2": "list2_value2-B"
"list2_key3": "list2_value3-B"
"usecase_key4": "usecase-value4-B"
"list2_key5": "list2_value5-B"
"list2_key6": "list2_value6-B"
}
]
Expected result:
"new_list": [
{
"list1_key1": "list1_value1-A"
"list1_key2": "list1_value2-A"
"list1_key3": "list1_value3-A"
"usecase_key4": "usecase_value4-A"
"list1_key5": "list1_value5-A"
"list1_key6": "list1_value6-A"
"list1_key7": "list1_value7-A"
"list1_key8": "list1_value8-A"
"list1_key9": "list1_value9-A"
"list1_key10":"list1_value10-A"
"list2_key1": "list2_value1-A"
"list2_key2": "list2_value2-A"
"list2_key3": "list2_value3-A"
"list2_key5": "list2_value5-A"
"list2_key6": "list2_value6-A"
},
{
"list1_key1": "list1_value1-B"
"list1_key2": "list1_value2-B"
"list1_key3": "list1_value3-B"
"usecase_key4": "usecase_value4-B"
"list1_key5": "list1_value5-B"
"list1_key6": "list1_value6-B"
"list1_key7": "list1_value7-B"
"list1_key8": "list1_value8-B"
"list1_key9": "list1_value9-B"
"list1_key10": "list1_value10-B"
"list2_key1": "list2_value1-B"
"list2_key2": "list2_value2-B"
"list2_key3": "list2_value3-B"
"list2_key5": "list2_value5-B"
"list2_key6": "list2_value6-B"
}
]
你有一个具体的问题,这个剧本可以解决这个问题:
- name: testplaybook
hosts: localhost
gather_facts: no
vars:
list_1:
- list1_key1: list1_value1-A
list1_key2: list1_value2-A
list1_key3: list1_value3-A
usecase_key4: usecase_value4-A
list1_key5: list1_value5-A
list1_key6: list1_value6-A
list1_key7: list1_value7-A
list1_key8: list1_value8-A
list1_key9: list1_value9-A
list1_key10: list1_value10-A
- list1_key1: list1_value1-B
list1_key2: list1_value2-B
list1_key3: list1_value3-B
usecase_key4: usecase_value4-B
list1_key5: list1_value5-B
list1_key6: list1_value6-B
list1_key7: list1_value7-B
list1_key8: list1_value8-B
list1_key9: list1_value9-B
list1_key10: list1_value10-B
list_2:
- list2_key1: list2_value1-A
list2_key2: list2_value2-A
list2_key3: list2_value3-A
usecase_key4: usecase_value4-A
list2_key5: list2_value5-A
list2_key6: list2_value6-A
- list2_key1: list2_value1-B
list2_key2: list2_value2-B
list2_key3: list2_value3-B
usecase_key4: usecase-value4-B
list2_key5: list2_value5-B
list2_key6: list2_value6-B
tasks:
- name: group
set_fact:
new_list: "{{ new_list | d([]) + [item.0 | combine(item.1)] }}"
loop: "{{ list_1 | zip(list_2) }}"
- name: group
debug:
var: new_list
结果:
"new_list": [
{
"list1_key1": "list1_value1-A",
"list1_key10": "list1_value10-A",
"list1_key2": "list1_value2-A",
"list1_key3": "list1_value3-A",
"list1_key5": "list1_value5-A",
"list1_key6": "list1_value6-A",
"list1_key7": "list1_value7-A",
"list1_key8": "list1_value8-A",
"list1_key9": "list1_value9-A",
"list2_key1": "list2_value1-A",
"list2_key2": "list2_value2-A",
"list2_key3": "list2_value3-A",
"list2_key5": "list2_value5-A",
"list2_key6": "list2_value6-A",
"usecase_key4": "usecase_value4-A"
},
{
"list1_key1": "list1_value1-B",
"list1_key10": "list1_value10-B",
"list1_key2": "list1_value2-B",
"list1_key3": "list1_value3-B",
"list1_key5": "list1_value5-B",
"list1_key6": "list1_value6-B",
"list1_key7": "list1_value7-B",
"list1_key8": "list1_value8-B",
"list1_key9": "list1_value9-B",
"list2_key1": "list2_value1-B",
"list2_key2": "list2_value2-B",
"list2_key3": "list2_value3-B",
"list2_key5": "list2_value5-B",
"list2_key6": "list2_value6-B",
"usecase_key4": "usecase-value4-B"
}
]
}
第一次发帖,轻点。 寻求有关如何完成以下任务的帮助。
2 个列表 1 个在列表
之间共享 key/value寻找如何根据两个列表中存在的一个 1 共享 key/value 将 list1 的所有元素/(key/value 元素?)关联到 list2 使用:usecase_key4 因为他们共享密钥 / usecase_valuse4-A / B 对于示例数据,值会更改但对于关联所需的数据集将相同。
感谢任何反馈
根据回复修改。 我尝试了 [ selectattr 和 json_query ] 的一些变体 不确定这是否是正确的方法。
"list-1": [
{
"list1_key1": "list1_value1-A"
"list1_key2": "list1_value2-A"
"list1_key3": "list1_value3-A"
"usecase_key4": "usecase_value4-A"
"list1_key5": "list1_value5-A"
"list1_key6": "list1_value6-A"
"list1_key7": "list1_value7-A"
"list1_key8": "list1_value8-A"
"list1_key9": "list1_value9-A"
"list1_key10":"list1_value10-A"
},
{
"list1_key1": "list1_value1-B"
"list1_key2": "list1_value2-B"
"list1_key3": "list1_value3-B"
"usecase_key4": "usecase_value4-B"
"list1_key5": "list1_value5-B"
"list1_key6": "list1_value6-B"
"list1_key7": "list1_value7-B"
"list1_key8": "list1_value8-B"
"list1_key9": "list1_value9-B"
"list1_key10": "list1_value10-B"
}
]
"list-2": [
{
"list2_key1": "list2_value1-A"
"list2_key2": "list2_value2-A"
"list2_key3": "list2_value3-A"
"usecase_key4": "usecase_value4-A"
"list2_key5": "list2_value5-A"
"list2_key6": "list2_value6-A"
},
{
"list2_key1": "list2_value1-B"
"list2_key2": "list2_value2-B"
"list2_key3": "list2_value3-B"
"usecase_key4": "usecase-value4-B"
"list2_key5": "list2_value5-B"
"list2_key6": "list2_value6-B"
}
]
Expected result:
"new_list": [
{
"list1_key1": "list1_value1-A"
"list1_key2": "list1_value2-A"
"list1_key3": "list1_value3-A"
"usecase_key4": "usecase_value4-A"
"list1_key5": "list1_value5-A"
"list1_key6": "list1_value6-A"
"list1_key7": "list1_value7-A"
"list1_key8": "list1_value8-A"
"list1_key9": "list1_value9-A"
"list1_key10":"list1_value10-A"
"list2_key1": "list2_value1-A"
"list2_key2": "list2_value2-A"
"list2_key3": "list2_value3-A"
"list2_key5": "list2_value5-A"
"list2_key6": "list2_value6-A"
},
{
"list1_key1": "list1_value1-B"
"list1_key2": "list1_value2-B"
"list1_key3": "list1_value3-B"
"usecase_key4": "usecase_value4-B"
"list1_key5": "list1_value5-B"
"list1_key6": "list1_value6-B"
"list1_key7": "list1_value7-B"
"list1_key8": "list1_value8-B"
"list1_key9": "list1_value9-B"
"list1_key10": "list1_value10-B"
"list2_key1": "list2_value1-B"
"list2_key2": "list2_value2-B"
"list2_key3": "list2_value3-B"
"list2_key5": "list2_value5-B"
"list2_key6": "list2_value6-B"
}
]
你有一个具体的问题,这个剧本可以解决这个问题:
- name: testplaybook
hosts: localhost
gather_facts: no
vars:
list_1:
- list1_key1: list1_value1-A
list1_key2: list1_value2-A
list1_key3: list1_value3-A
usecase_key4: usecase_value4-A
list1_key5: list1_value5-A
list1_key6: list1_value6-A
list1_key7: list1_value7-A
list1_key8: list1_value8-A
list1_key9: list1_value9-A
list1_key10: list1_value10-A
- list1_key1: list1_value1-B
list1_key2: list1_value2-B
list1_key3: list1_value3-B
usecase_key4: usecase_value4-B
list1_key5: list1_value5-B
list1_key6: list1_value6-B
list1_key7: list1_value7-B
list1_key8: list1_value8-B
list1_key9: list1_value9-B
list1_key10: list1_value10-B
list_2:
- list2_key1: list2_value1-A
list2_key2: list2_value2-A
list2_key3: list2_value3-A
usecase_key4: usecase_value4-A
list2_key5: list2_value5-A
list2_key6: list2_value6-A
- list2_key1: list2_value1-B
list2_key2: list2_value2-B
list2_key3: list2_value3-B
usecase_key4: usecase-value4-B
list2_key5: list2_value5-B
list2_key6: list2_value6-B
tasks:
- name: group
set_fact:
new_list: "{{ new_list | d([]) + [item.0 | combine(item.1)] }}"
loop: "{{ list_1 | zip(list_2) }}"
- name: group
debug:
var: new_list
结果:
"new_list": [
{
"list1_key1": "list1_value1-A",
"list1_key10": "list1_value10-A",
"list1_key2": "list1_value2-A",
"list1_key3": "list1_value3-A",
"list1_key5": "list1_value5-A",
"list1_key6": "list1_value6-A",
"list1_key7": "list1_value7-A",
"list1_key8": "list1_value8-A",
"list1_key9": "list1_value9-A",
"list2_key1": "list2_value1-A",
"list2_key2": "list2_value2-A",
"list2_key3": "list2_value3-A",
"list2_key5": "list2_value5-A",
"list2_key6": "list2_value6-A",
"usecase_key4": "usecase_value4-A"
},
{
"list1_key1": "list1_value1-B",
"list1_key10": "list1_value10-B",
"list1_key2": "list1_value2-B",
"list1_key3": "list1_value3-B",
"list1_key5": "list1_value5-B",
"list1_key6": "list1_value6-B",
"list1_key7": "list1_value7-B",
"list1_key8": "list1_value8-B",
"list1_key9": "list1_value9-B",
"list2_key1": "list2_value1-B",
"list2_key2": "list2_value2-B",
"list2_key3": "list2_value3-B",
"list2_key5": "list2_value5-B",
"list2_key6": "list2_value6-B",
"usecase_key4": "usecase-value4-B"
}
]
}