-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory.py
More file actions
executable file
·62 lines (58 loc) · 1.91 KB
/
inventory.py
File metadata and controls
executable file
·62 lines (58 loc) · 1.91 KB
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
from pathlib import Path
import json
import subprocess
import sys
import os
from collections import defaultdict
# multipassのVMのリストを取得するコマンド
cmd = ['multipass', 'list', '--format', 'json']
result = subprocess.run(cmd, stdout=subprocess.PIPE)
# multipassコマンドの出力をJSONデータに変換する
instances = json.loads(result.stdout.decode('utf-8'))
inventory = {
"all": {
"vars": {
'ansible_port': 22,
"ansible_ssh_user": "ubuntu",
'ansible_ssh_private_key_file': str(Path(os.path.dirname(__file__))/"multipass.key"),
}
},
"clients": {
"children": ["mons", "mgrs", "osds"],
},
"mons": {
"hosts": [],
},
"osds": {
"hosts": [],
},
"admin": {
"hosts": [],
},
"_meta": {
"hostvars": defaultdict(dict),
},
}
_meta_vars = inventory['_meta']['hostvars']
for instance in instances["list"]:
name = instance["name"]
key = f"{name[:3]}s"
if name == "mon1":
key = "admin"
inventory[key]["hosts"].append(name)
_meta_vars[name]["ansible_host"] = instance['ipv4'][0]
# Write by ChatGPT-3
if len(sys.argv) == 2 and sys.argv[1] == '--list' or len(sys.argv) < 2:
# --listフラグが渡された場合、ホストリスト全体を出力する
print(json.dumps(inventory))
elif len(sys.argv) == 3 and sys.argv[1] == '--host':
# --hostフラグが渡された場合、指定されたホストの情報を出力する
hostname = sys.argv[2]
host = next((hostname for instance in instances["list"] if instance["name"] == hostname), None)
if host:
print(json.dumps({'_meta': {'hostvars': _meta_vars[host]}, 'host': host}))
else:
print(json.dumps({'_meta': {'hostvars': {}}, 'host': {}}))
else:
# それ以外の場合は空のJSONを出力する
print(json.dumps({'_meta': {'hostvars': {}}, 'all': {'hosts': []}}))