【问题标题】:Combine two ansible dictionaries by common value in key通过 key 中的共同值组合两个 ansible 字典
【发布时间】:2019-06-21 15:38:51
【问题描述】:

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

"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"}]"

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

【问题讨论】:

  • this 有帮助吗?

标签: python ansible


【解决方案1】:

这是怎么做的:

  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]$ 

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2017-11-07
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 2015-10-07
    • 2017-11-21
    相关资源
    最近更新 更多