动态 ansible 库存 JSON 用 pandas 标准化
dynamic ansible inventory JSON normalized with pandas
我在使用 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
这是我第一次接触 pandas 并且很容易实现。您可能需要进行更多调整并更改 read/store 数据的方式来满足您的确切要求,但这完成了大部分工作。
示例作为示例 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()
我在使用 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 |
这是我第一次接触 pandas 并且很容易实现。您可能需要进行更多调整并更改 read/store 数据的方式来满足您的确切要求,但这完成了大部分工作。
示例作为示例 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()