This repository was archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathinventory
More file actions
executable file
·57 lines (47 loc) · 1.57 KB
/
inventory
File metadata and controls
executable file
·57 lines (47 loc) · 1.57 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
#!/usr/bin/env python3
# SPDX-License-Identifier: CC0-1.0
import yaml
import os
import json
import argparse
import collections as coll
inventory_defaults = {
'ansible_ssh_port': 22,
'ansible_ssh_user': 'root',
}
ansibledir = os.path.dirname(os.path.realpath(__file__))
host_vars = os.path.join(ansibledir, 'host_vars')
ansible_groups = coll.defaultdict(list)
ansible_hosts = {}
for entry in os.scandir(host_vars):
if entry.name.endswith('.yml'):
host = entry.name[:-4]
yml = {}
with open(entry.path, 'r') as stream:
yml = yaml.safe_load(stream)
ansible_hosts[host] = { 'ansible_host': host }
for group in yml.get('pull_groups', []):
ansible_groups[group].append(host)
parser = argparse.ArgumentParser(
description='convert ansible-pull data into push inventory'
)
pgroup = parser.add_mutually_exclusive_group(required=True)
pgroup.add_argument('-l', '--list', action='store_true', help='list inventory')
pgroup.add_argument('-H', '--host', action='store', help='get a host\'s variables')
args = parser.parse_args()
if args.host:
print(json.dumps(ansible_hosts[args.host]))
elif args.list:
# format ref: https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html
print(json.dumps({
'_meta': {
'hostvars': { **ansible_hosts }
},
'all': {
'hosts': list(ansible_hosts.keys()),
'vars': inventory_defaults,
},
**{x[0]: {'hosts': x[1]} for x in ansible_groups.items()}
}))
else:
raise NotImplementedError()