This repository has been archived by the owner on Aug 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform.py
executable file
·113 lines (89 loc) · 3.24 KB
/
terraform.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
113
#! /usr/bin/env python2
import json
import os
import re
import subprocess
import sys
TERRAFORM_PATH = os.environ.get('ANSIBLE_TF_BIN', 'terraform')
TERRAFORM_DIR = os.environ.get('ANSIBLE_TF_DIR', os.getcwd())
def _extract_dict(attrs, key):
out = {}
for k in attrs.keys():
match = re.match(r"^" + key + r"\.(.*)", k)
if not match or match.group(1) == "%":
continue
out[match.group(1)] = attrs[k]
return out
def _extract_list(attrs, key):
out = []
length_key = key + ".#"
if length_key not in attrs.keys():
return []
length = int(attrs[length_key])
if length < 1:
return []
for i in range(0, length):
out.append(attrs["{}.{}".format(key, i)])
return out
def _init_group(children=None, hosts=None, vars=None):
return {
"hosts": [] if hosts is None else hosts,
"vars": {} if vars is None else vars,
"children": [] if children is None else children
}
def _add_host(inventory, hostname, groups, host_vars):
inventory["_meta"]["hostvars"][hostname] = host_vars
for group in groups:
if group not in inventory.keys():
inventory[group] = _init_group(hosts=[hostname])
elif hostname not in inventory[group]:
inventory[group]["hosts"].append(hostname)
def _add_group(inventory, group_name, children, group_vars):
if group_name not in inventory.keys():
inventory[group_name] = _init_group(children=children, vars=group_vars)
else:
# Start out with support for only one "group" with a given name
# If there's a second group by the name, last in wins
inventory[group_name]["children"] = children
inventory[group_name]["vars"] = group_vars
def _init_inventory():
return {
"all": _init_group(),
"_meta": {
"hostvars": {}
}
}
def _handle_host(attrs, inventory):
host_vars = _extract_dict(attrs, "vars")
groups = _extract_list(attrs, "groups")
hostname = attrs["inventory_hostname"]
if "all" not in groups:
groups.append("all")
_add_host(inventory, hostname, groups, host_vars)
def _handle_group(attrs, inventory):
group_vars = _extract_dict(attrs, "vars")
children = _extract_list(attrs, "children")
group_name = attrs["inventory_group_name"]
_add_group(inventory, group_name, children, group_vars)
def _walk_state(tfstate, inventory):
for module in tfstate["modules"]:
for resource in module["resources"].values():
if not resource["type"].startswith("ansible_"):
continue
attrs = resource["primary"]["attributes"]
if resource["type"] == "ansible_host":
_handle_host(attrs, inventory)
if resource["type"] == "ansible_group":
_handle_group(attrs, inventory)
return inventory
def _main():
try:
tf_command = [TERRAFORM_PATH, 'state', 'pull', '-input=false']
proc = subprocess.Popen(tf_command, cwd=TERRAFORM_DIR, stdout=subprocess.PIPE)
tfstate = json.load(proc.stdout)
inventory = _walk_state(tfstate, _init_inventory())
sys.stdout.write(json.dumps(inventory, indent=2))
except:
sys.exit(1)
if __name__ == '__main__':
_main()