-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscw_inventory.py
executable file
·102 lines (91 loc) · 2.83 KB
/
scw_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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Generate an inventory of servers from scaleway which is
suitable for use as an ansible dynamic inventory
Right now, only the group 'cci-customer' is exported
'''
import argparse
import json
import configparser
from scaleway.apis import ComputeAPI
class SCWInventory(object):
'''
The inventory class which calls out to scaleway and digests
the returned data, making it usable by ansible as an inventory
'''
def __init__(self):
self.args = None
self.inventory = None
self.auth_token = None
self.response = {
'_meta': {
'hostvars': {
}
}
}
def parse_config(self, creds_file='scw_credentials.ini'):
'''
Parse the ini file to get the auth token
'''
config = configparser.ConfigParser()
config.read(creds_file)
self.auth_token = config['credentials']['auth_token']
def parse_arguments(self):
'''
Use argparse to interpret flags passed into the script
'''
parser = argparse.ArgumentParser(
description='generate an ansible dnyamic inventory from scaleway api'
)
parser.add_argument(
"--list",
help="generate a json list of hosts and groups",
action="store_true",
default=True
)
self.args = parser.parse_args()
def get_servers(self):
'''
query scaleway api and pull down a list of servers
'''
self.parse_config()
api = ComputeAPI(auth_token=self.auth_token)
result = api.query().servers.get()
self.inventory = [
[i['name'], i['public_ip'], i['tags']] for i in result['servers']
]
for host, ip_info, tags in self.inventory:
if ip_info:
self.response['_meta']['hostvars'][host] = {
'ansible_host': ip_info['address']
}
if tags:
for tag in tags:
if tag.startswith('group:'):
self._add_to_response(
tag.split(':')[1],
host
)
def _add_to_response(self, group, hostname):
'''
add a host to a group within the response
'''
if group not in self.response:
self.response[group] = list()
if group in self.response:
self.response[group].append(hostname)
def print_inventory(self):
'''
simply display the collected inventory
'''
print(json.dumps(self.response))
def main():
'''
run the program starting here
'''
inventory = SCWInventory()
inventory.get_servers()
inventory.print_inventory()
if __name__ == '__main__':
main()