【问题标题】:dynamic ansible inventory JSON normalized with pandas使用 pandas 规范化的动态 ansible 库存 JSON
【发布时间】:2021-07-23 10:53:11
【问题描述】:

我在使用 pandas 数据框规范化 ansible-inventory 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"
        ]
    }
}

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

ansible_host portal_cpu portal_domain portal_group portal_hostname portal_ip_address portal_memory
10.10.252.78 2 prod.local DBA myhostscdb02 10.10.252.78 8

【问题讨论】:

    标签: python json pandas ansible inventory


    【解决方案1】:

    这是我第一次接触 pandas,而且很容易实现。您可能需要进行更多调整并更改读取/存储数据的方式以满足您的确切要求,但这就是大部分工作。

    作为示例 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-26
      • 2020-12-11
      • 2019-12-22
      • 2020-06-21
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多