用pandas规范化动态ansible库存

2024-09-30 16:37:53 发布

您现在位置:Python中文网/ 问答频道 /正文

使用熊猫数据帧规范ansible清单JSON时遇到问题

跑步后

ansible-inventory -i dynamic_inventory.yaml --playbook-dir ./ --list > list.json

我明白了

{
    "_meta": {
        "hostvars": {
            "myhostdcdb01": {
                "ansible_host": "10.10.252.66",
                "portal_cpu": 2,
                "portal_domain": "prod.local",
                "portal_group": "DBA",
                "portal_hostname": "myhostdcdb01",
                "portal_ip_address": "10.10.252.66",
                "portal_memory": 8
            },
            "myhostdcdb02": {
                "ansible_host": "10.10.252.67",
                "portal_cpu": 2,
                "portal_domain": "prod.local",
                "portal_group": "DBA",
                "portal_hostname": "myhostdcdb02",
                "portal_ip_address": "10.10.252.67",
                "portal_memory": 8
            },
            "myhostdcdb03": {
                "ansible_host": "10.10.252.68",
                "portal_cpu": 2,
                "portal_domain": "prod.local",
                "portal_group": "DBA",
                "portal_hostname": "myhostdcdb03",
                "portal_ip_address": "10.10.252.68",
                "portal_memory": 8
            },
            "myhostscdb01": {
                "ansible_host": "10.10.252.76",
                "portal_cpu": 2,
                "portal_domain": "prod.local",
                "portal_group": "DBA",
                "portal_hostname": "myhostscdb01",
                "portal_ip_address": "10.10.252.76",
                "portal_memory": 8
            },
            "myhostscdb02": {
                "ansible_host": "10.10.252.78",
                "portal_cpu": 2,
                "portal_domain": "prod.local",
                "portal_group": "DBA",
                "portal_hostname": "myhostscdb02",
                "portal_ip_address": "10.10.252.78",
                "portal_memory": 8
            },
            "myhostscdb03": {
                "ansible_host": "10.10.252.80",
                "portal_cpu": 2,
                "portal_domain": "prod.local",
                "portal_group": "DBA",
                "portal_hostname": "myhostscdb03",
                "portal_ip_address": "10.10.252.80",
                "portal_memory": 8
            }
        }
    },
    "all": {
        "children": [
            "ungrouped"
        ]
    },
    "ungrouped": {
        "hosts": [
            "myhostdcdb01",
            "myhostdcdb02",
            "myhostdcdb03",
            "myhostscdb01",
            "myhostscdb02",
            "myhostscdb03"
        ]
    }
}

现在我不知道如何将其规范化为一种合理的格式,其中每个主机及其详细信息位于单独的一行中,即:

^{tb1}$

Tags: iphostaddressdomainlocalgroupprodansible
1条回答
网友
1楼 · 发布于 2024-09-30 16:37:53

这是我第一次接触熊猫,很容易做到。您可能需要进行更多的调整,并改变读取/存储数据的方式,以满足您的确切需求,但这将使大部分工作变得更加重要

示例作为示例python交互式会话

$ python
Python 3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import pandas
>>>
>>> # Load original data. Read that from your command or a file
>>> inventory_data = {
...     "_meta": {
...         "hostvars": {
...             "myhostdcdb01": {
...                 "ansible_host": "10.10.252.66",
...                 "portal_cpu": 2,
...                 "portal_domain": "prod.local",
...                 "portal_group": "DBA",
...                 "portal_hostname": "myhostdcdb01",
...                 "portal_ip_address": "10.10.252.66",
...                 "portal_memory": 8
...             },
...             "myhostdcdb02": {
...                 "ansible_host": "10.10.252.67",
...                 "portal_cpu": 2,
...                 "portal_domain": "prod.local",
...                 "portal_group": "DBA",
...                 "portal_hostname": "myhostdcdb02",
...                 "portal_ip_address": "10.10.252.67",
...                 "portal_memory": 8
...             },
...             "myhostdcdb03": {
...                 "ansible_host": "10.10.252.68",
...                 "portal_cpu": 2,
...                 "portal_domain": "prod.local",
...                 "portal_group": "DBA",
...                 "portal_hostname": "myhostdcdb03",
...                 "portal_ip_address": "10.10.252.68",
...                 "portal_memory": 8
...             },
...             "myhostscdb01": {
...                 "ansible_host": "10.10.252.76",
...                 "portal_cpu": 2,
...                 "portal_domain": "prod.local",
...                 "portal_group": "DBA",
...                 "portal_hostname": "myhostscdb01",
...                 "portal_ip_address": "10.10.252.76",
...                 "portal_memory": 8
...             },
...             "myhostscdb02": {
...                 "ansible_host": "10.10.252.78",
...                 "portal_cpu": 2,
...                 "portal_domain": "prod.local",
...                 "portal_group": "DBA",
...                 "portal_hostname": "myhostscdb02",
...                 "portal_ip_address": "10.10.252.78",
...                 "portal_memory": 8
...             },
...             "myhostscdb03": {
...                 "ansible_host": "10.10.252.80",
...                 "portal_cpu": 2,
...                 "portal_domain": "prod.local",
...                 "portal_group": "DBA",
...                 "portal_hostname": "myhostscdb03",
...                 "portal_ip_address": "10.10.252.80",
...                 "portal_memory": 8
...             }
...         }
...     },
...     "all": {
...         "children": [
...             "ungrouped"
...         ]
...     },
...     "ungrouped": {
...         "hosts": [
...             "myhostdcdb01",
...             "myhostdcdb02",
...             "myhostdcdb03",
...             "myhostscdb01",
...             "myhostscdb02",
...             "myhostscdb03"
...         ]
...     }
... }
>>>
>>> # Load hostvars in a dataframe + switch lines/columns
>>> df = pandas.DataFrame.from_dict(inventory_data['_meta']['hostvars']).transpose()
>>>
>>> # Have a look at result
>>> print(df)
              ansible_host portal_cpu portal_domain portal_group portal_hostname portal_ip_address portal_memory
myhostdcdb01  10.10.252.66          2    prod.local          DBA    myhostdcdb01      10.10.252.66             8
myhostdcdb02  10.10.252.67          2    prod.local          DBA    myhostdcdb02      10.10.252.67             8
myhostdcdb03  10.10.252.68          2    prod.local          DBA    myhostdcdb03      10.10.252.68             8
myhostscdb01  10.10.252.76          2    prod.local          DBA    myhostscdb01      10.10.252.76             8
myhostscdb02  10.10.252.78          2    prod.local          DBA    myhostscdb02      10.10.252.78             8
myhostscdb03  10.10.252.80          2    prod.local          DBA    myhostscdb03      10.10.252.80             8
>>> exit()

相关问题 更多 >