Python 按键中的公共值组合两个ansible字典

Python 按键中的公共值组合两个ansible字典,python,ansible,Python,Ansible,我有两个可靠的事实,一个看起来像这样: "ansible_facts": "int_table" { "[{ "connection": "notconnected", "port": "eth1"}, { "connection": "connected", "port": "eth2"}]" 及 我想创建一个新的事实,通过它们的端口将二者结合起来,以便输出 "ansible_facts": "new_table" { "[{ "m

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

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

我想创建一个新的事实,通过它们的端口将二者结合起来,以便输出

"ansible_facts": "new_table" {
    "[{ "mac_address": "0000.c200.0101",
        "connection": "notconnected",
        "port": "eth1"},
      { "mac_address": "0320.c500.0201",
        "connection": "connected",
        "port": "eth2"}]"
用纯ansible可以吗?我已经尝试将这两个事实都传递给自定义过滤器,以使用python将两者结合起来,但似乎无法将两个事实传递给同一个过滤器。

以下是如何做到这一点:

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

  • 对于每个端口,从
    int\u表
    中找到元素,并将其与相应的from
    mac\u表

  • 打印最终列表变量

  • 剧本:

    ---
    - 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]$ 
    
    希望有帮助

    有帮助吗?
    [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]$