gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:12:38 25 4
gpt4 key购买 nike

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

这里是输入数据,它是一个列表的列表。

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

但是,如果 key 相同,我想创建一个 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这些字符串,和/或,使用 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] }}"
  • 使用分割:

    - 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

关于ansible - 从变量创建字典键并添加 IP 列表作为值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72057036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com