通过键中的公共值组合两个可操作词典

时间:2019-06-21 15:38:51

标签: python ansible

我有两个事实,一个看起来像这样:

"ansible_facts": "int_table" {
    "[{ "connection": "notconnected",
        "port": "eth1"},
      { "connection": "connected",
        "port": "eth2"}]"

"ansible_facts": "mac_table" {
    "[{ "mac_address": "0000.c200.0101",
        "port": "eth1"},
      { "mac_address": "0320.c500.0201",
        "port": "eth2"}]"

我想创建一个新的事实,将它们的端口组合在一起,以便输出

"ansible_facts": "new_table" {
    "[{ "mac_address": "0000.c200.0101",
        "connection": "notconnected",
        "port": "eth1"},
      { "mac_address": "0320.c500.0201",
        "connection": "connected",
        "port": "eth2"}]"

这是否有可能做到?我尝试将两者都传递给自定义过滤器,以使用python将两者结合起来,但似乎无法将两个事实传递给同一过滤器。

1 个答案:

答案 0 :(得分:2)

操作方法如下:

  1. 从1个变量获取端口列表,例如int_table,它们应该是唯一的端口(即,每个列表中只有一个元素可以具有eth1,eth2等)

  2. 对于每个端口,从int_table中查找元素,并将其与mac_table中的相应元素组合

  3. 打印最终列表变量

剧本:

---
- hosts: localhost
  gather_facts: false
  vars:
    int_table:
    - connection: notconnected
      port: eth1
    - connection: connected
      port: eth2
    mac_table:
    - mac_address: 0000.c200.0101
      port: eth1
    - mac_address: 0320.c500.0201
      port: eth2


  tasks:

  - name: populate merged list
    set_fact: 
      final_var: "{{ final_var | default([]) + [int_table | selectattr('port','equalto', item) | first | combine(mac_table | selectattr('port','equalto', item) | first)] }}"
    with_items: 
    - "{{ int_table | map(attribute='port') | list }}"

  - name: print merged list
    debug:
      var: final_var

示例输出:

[http_offline@greenhat-29 tests]$ ansible-playbook test.yml 

PLAY [localhost] *******************************************************************************************************************************************************************************************************

TASK [populate merged list] ********************************************************************************************************************************************************************************************
ok: [localhost] => (item=eth1)
ok: [localhost] => (item=eth2)

TASK [print merged list] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
    "final_var": [
        {
            "connection": "notconnected",
            "mac_address": "0000.c200.0101",
            "port": "eth1"
        },
        {
            "connection": "connected",
            "mac_address": "0320.c500.0201",
            "port": "eth2"
        }
    ]
}

PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[http_offline@greenhat-29 tests]$ 

希望有帮助