从变量创建字典键并将 IP 列表添加为值

Create dictionary keys from variable and add list of IPs as values

我想创建一个字典并从变量中填充键和值。这些值将是 IP 列表。
如果密钥已经存在于字典中,那么,我需要将 IP 添加到现有列表中。

这里是输入数据,是list of list

parse1.json:

[
    [
        "/RAD33G4099/mobile_app/pl_crad.la-dc.vivo.com_80",
        "properties",
        "members",
        "/Common/10.50.100.25:80",
        "session"
    ],
    [
        "/RAD33G4099/mobile_app/pl_crad.la-dc.vivo.com_80",
        "properties",
        "members",
        "/Common/10.50.100.39:80",
        "session"
    ],
    [
        "/RAD33G4099/mobile_app/pl_crad.la-dc.vivo.com_80",
        "properties",
        "members",
        "/Common/10.50.100.49:80",
        "session"
    ],
    [
        "/RAD33G4099/mobile_app/pl_crad.la-dc.vivo.com_443",
        "properties",
        "members",
        "/Common/10.50.100.18:80",
        "session"
    ],
    [
        "/RAD33G4099/mobile_app/pl_crad.la-dc.vivo.com_443",
        "properties",
        "members",
        "/Common/10.50.100.28:80",
        "session"
    ],
    [
        "/RAD33G4099/mobile_app/pl_crad.la-dc.vivo.com_443",
        "properties",
        "members",
        "/Common/10.50.100.48:80",
        "session"
    ]
]

这是我解决它的尝试:

 - set_fact:
     my_dic: {} 
     disabled_host: "{{ lookup('file', 'parse1.json')   }}"  

 - name: add new key / value pairs to dict
      set_fact:
        my_dict_var: "{{ my_dict_var + [ my_dic | combine ( { name + '_' +  app_name : host } ,  recursive=True) ] }}"
      loop: "{{ disabled_host }}"
      vars:
        name: "{{ item[0]  | regex_replace('^\/.*\/(.*)$', '\1') }}"
        app_name: "{{ item[0] | regex_replace('^\/.*\/(.*)\/.*$', '\1')  }}"
        host: "{{ item[3] | regex_replace('^\/.*\/(.*):.*$', '\1')  }}"

实际输出为

ok: [localhost] => {
    "msg": [
        {
            "pl_crad.la-dc.vivo.com_80_mobile_app": "10.50.100.25"
        },
        {
            "pl_crad.la-dc.vivo.com_80_mobile_app": "10.50.100.39"
        },
        {
            "pl_crad.la-dc.vivo.com_80_mobile_app": "10.50.100.49"
        },
        {
            "pl_crad.la-dc.vivo.com_443_mobile_app": "10.50.100.18"
        },
        {
            "pl_crad.la-dc.vivo.com_443_mobile_app": "10.50.100.28"
        },
        {
            "pl_crad.la-dc.vivo.com_443_mobile_app": "10.50.100.48"
        }
    ]
}

但是,如果密钥相同,我想创建一个 IP 列表。

我想要的输出是

{
  "pl_crad.la-dc.vivo.com_80_mobile_app": [ 
    "10.50.100.25", 
    "10.50.100.39", 
    "10.50.100.49" 
  ],
  "pl_crad.la-dc.vivo.com_443_mobile_app": [ 
    "10.50.100.18",
    "10.50.100.28", 
    "10.50.100.48"
  ]
}

您不必费力地使用正则表达式,只需 split those strings, and / or, use basename and dirname.

  • 使用 dirnamebasename:

    - set_fact:
      disabled_hosts_ips: >-
        {{
          disabled_hosts_ips | default({})
          | combine({
            name: (disabled_hosts_ips | default({}))[name] | default([]) 
              + [ip] 
          })
        }}
      loop: "{{ disabled_hosts }}"
      vars:
        name: "{{ item[0] | basename ~ '_' ~ item[0] | dirname | basename }}"
        ip: "{{ (item[3] | basename).split(':')[0] }}"
    
  • 使用split:

    - set_fact:
      disabled_hosts_ips: >-
        {{
          disabled_hosts_ips | default({})
          | combine({
            name: (disabled_hosts_ips | default({}))[name] | default([]) 
              + [ip]
          })
        }}
      loop: "{{ disabled_hosts }}"
      vars:
        name: "{{ item[0].split('/')[-2:] | reverse | join('_') }}"
        ip: "{{ item[3].split('/')[-1].split(':')[0] }}"
    

两者都给出:

disabled_hosts_ips:
  pl_crad.la-dc.vivo.com_443_mobile_app:
  - 10.50.100.18
  - 10.50.100.28
  - 10.50.100.48
  pl_crad.la-dc.vivo.com_80_mobile_app:
  - 10.50.100.25
  - 10.50.100.39
  - 10.50.100.49

例如,使用变量my_dict_var

  my_dict_groups: "{{ dict(my_dict_keys|zip(my_dict_vals)) }}"
  my_dict_keys: "{{ dh_groups|map('first')|list }}"
  my_dict_vals: "{{ dh_groups|map('last')|map('map', attribute='value')|list }}"
  dh_groups: "{{ my_dict_var|map('dict2items')|flatten|groupby('key') }}"

给予

  my_dict_groups:
    pl_crad.la-dc.vivo.com_443_mobile_app:
    - 10.50.100.18
    - 10.50.100.28
    - 10.50.100.48
    pl_crad.la-dc.vivo.com_80_mobile_app:
    - 10.50.100.25
    - 10.50.100.39
    - 10.50.100.49

完整的声明集如下

  my_dict_groups: "{{ dict(my_dict_keys|zip(my_dict_vals)) }}"
  my_dict_keys: "{{ dh_groups|map('first')|list }}"
  my_dict_vals: "{{ dh_groups|map('last')|map('map', attribute='value')|list }}"
  dh_groups: "{{ dh_keys|zip(dh_vals)|map('combine')|groupby('key') }}"
  dh_vals: "{{ disabled_host|
               map(attribute=3)|
               map('split', '/')|map('last')|
               map('split', ':')|map('first')|
               map('community.general.dict_kv', 'value')|
               list }}"
  dh_keys: "{{ disabled_host|
               map(attribute=0)|
               map('regex_replace', dh_regex, dh_replace)|
               map('community.general.dict_kv', 'key')|
               list }}"
  dh_regex: '^/.*?/(.*?)/(.*)$'
  dh_replace: '_'
  disabled_host: "{{ lookup('file', 'parse1.json') }}"

详情

在这两种情况下都使用过滤器 groupby 并创建列表

  dh_groups:
  - - pl_crad.la-dc.vivo.com_443_mobile_app
    - - key: pl_crad.la-dc.vivo.com_443_mobile_app
        value: 10.50.100.18
      - key: pl_crad.la-dc.vivo.com_443_mobile_app
        value: 10.50.100.28
      - key: pl_crad.la-dc.vivo.com_443_mobile_app
        value: 10.50.100.48
  - - pl_crad.la-dc.vivo.com_80_mobile_app
    - - key: pl_crad.la-dc.vivo.com_80_mobile_app
        value: 10.50.100.25
      - key: pl_crad.la-dc.vivo.com_80_mobile_app
        value: 10.50.100.39
      - key: pl_crad.la-dc.vivo.com_80_mobile_app
        value: 10.50.100.49

创建键 my_dict_keys 和值 my_dict_vals 的列表,并创建字典 my_dict_groups

  my_dict_keys:
  - pl_crad.la-dc.vivo.com_443_mobile_app
  - pl_crad.la-dc.vivo.com_80_mobile_app
  my_dict_vals:
  - - 10.50.100.18
    - 10.50.100.28
    - 10.50.100.48
  - - 10.50.100.25
    - 10.50.100.39
    - 10.50.100.49

例如,将声明放入 playbook vars

- hosts: localhost

  vars:
    my_dict_groups: "{{ dict(my_dict_keys|zip(my_dict_vals)) }}"
    my_dict_keys: "{{ dh_groups|map('first')|list }}"
    my_dict_vals: "{{ dh_groups|map('last')|map('map', attribute='value')|list }}"
    # dh_groups: "{{ my_dict_var|map('dict2items')|flatten|groupby('key') }}"
    dh_groups: "{{ dh_keys|zip(dh_vals)|map('combine')|groupby('key') }}"
    dh_vals: "{{ disabled_host|
                 map(attribute=3)|
                 map('split', '/')|
                 map('last')|map('split', ':')|map('first')|
                 map('community.general.dict_kv', 'value')|
                 list }}"
    dh_keys: "{{ disabled_host|
                 map(attribute=0)|
                 map('regex_replace', dh_regex, dh_replace)|
                 map('community.general.dict_kv', 'key')|
                 list }}"
    dh_regex: '^/.*?/(.*?)/(.*)$'
    dh_replace: '_'
    disabled_host: "{{ lookup('file', 'parse1.json') }}"
    # my_dict_var:
    #   - pl_crad.la-dc.vivo.com_80_mobile_app: 10.50.100.25
    #   - pl_crad.la-dc.vivo.com_80_mobile_app: 10.50.100.39
    #   - pl_crad.la-dc.vivo.com_80_mobile_app: 10.50.100.49
    #   - pl_crad.la-dc.vivo.com_443_mobile_app: 10.50.100.18
    #   - pl_crad.la-dc.vivo.com_443_mobile_app: 10.50.100.28
    #   - pl_crad.la-dc.vivo.com_443_mobile_app: 10.50.100.48

  tasks:
    - debug:
        var: my_dict_groups