-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_inventory.py
executable file
·112 lines (86 loc) · 2.91 KB
/
get_inventory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
import sys
import json
import os
from proxmoxer import ProxmoxAPI
def get_nodes(node_name: str, user: str, token_name: str, token_value: str) -> list:
pve = ProxmoxAPI(host=node_name, user=f"{user}@pve", token_name=token_name, token_value=token_value)
cluster_nodes = [node for node in pve.nodes.get() if node['status'] == 'online']
vms = []
for node in cluster_nodes:
node_name = node['node']
node_vms = pve.nodes(node_name).qemu.get()
for vm in node_vms:
# Get only running nodes with tags
if vm['status'] == 'running' and 'tags' in vm:
vm_config = pve.nodes(node_name).qemu(vm['vmid']).config.get()
if 'ipconfig0' not in vm_config:
print(f"There is missing `ipconfig0` property for vm '{vm['name']}'!")
sys.exit(1)
# Extract IP from configuration (it cannot be IP from status due to keepalived's vip)
vm['ip'] = vm_config['ipconfig0'].split(',')[0].split('=')[1].split('/')[0]
# Save a node name for K8s labeling
vm['meta'] = {
'node_name': node_name
}
vms.append(vm)
return vms
def define_vars_on_tags(tags) -> (dict[str, str] | dict):
if 'k3s' in tags:
for subtag in tags:
if 'k3s_' in subtag:
parts = subtag.split('_')
if len(parts) == 3:
cluster_type = parts[0]
name = parts[1]
role = parts[2]
match role:
case 'cp':
cluster_role = 'server'
case 'lb':
cluster_role = 'load-balancer'
case _:
cluster_role = 'agent'
return {
'cluster_name': name,
'cluster_type': cluster_type,
'cluster_role': cluster_role
}
if 'vault' in tags:
return {
'cluster_name': 'vault',
'cluster_type': 'vault',
'cluster_role': 'member'
}
return {}
def get_inventory() -> dict[str, dict[str, dict]]:
nodes = get_nodes(
os.getenv('ANSIBLE_PROXMOX_HOST'),
os.getenv('ANSIBLE_PROXMOX_USER'),
os.getenv('ANSIBLE_PROXMOX_TOKEN_NAME'),
os.getenv('ANSIBLE_PROXMOX_TOKEN_VALUE'),
)
inventory = {'_meta': {'hostvars': {}}}
for node in nodes:
tags = node['tags'].split(';')
for tag in tags:
if tag not in inventory:
inventory[tag] = {'hosts': [], 'vars': {}}
inventory[tag]['hosts'].append(node['name'])
inventory['_meta']['hostvars'][node['name']] = {
'ansible_host': node['ip'],
'ansible_port': '22',
'ansible_user': 'vertisan',
'ansible_python_interpreter': '/usr/bin/python3',
'vars': {**define_vars_on_tags(tags), **node['meta']}
}
return inventory
def main() -> None:
if len(sys.argv) == 2 and (sys.argv[1] == '--list'):
inventory = get_inventory()
print(json.dumps(inventory, indent=2))
else:
print("Usage: --list")
sys.exit(1)
if __name__ == '__main__':
main()