-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackl.py
83 lines (72 loc) · 3.05 KB
/
stackl.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
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = '''
name: stackl
plugin_type: inventory
author:
- Stef Graces <@stgrace>
- Frederic Van Reet <@GBrawl>
short_description: Stackl inventory
description:
- Fetch a Stack instance from Stackl.
- Uses a YAML configuration file that ends with C(stackl.(yml|yaml)).
options:
plugin:
description: Name of the plugin
required: true
choices: ['stackl']
host:
description: Stackl's host url
required: true
stack_instance:
description: Stack instance name
required: true
'''
EXAMPLES = '''
plugin: stackl
host: "http://localhost:8080"
stack_instance: "test_vm"
'''
import json
import stackl_client
from ansible.errors import AnsibleError, AnsibleParserError
from ansible.plugins.inventory import BaseInventoryPlugin
class InventoryModule(BaseInventoryPlugin):
NAME = 'stackl'
def verify_file(self, path):
''' return true/false if this is possibly a valid file for this plugin to consume '''
valid = False
if super(InventoryModule, self).verify_file(path):
# base class verifies that file exists and is readable by current user
if path.endswith(('stackl.yaml', 'stackl.yml')):
valid = True
return valid
def parse(self, inventory, loader, path, cache):
super(InventoryModule, self).parse(inventory, loader, path, cache)
self._read_config_data(path)
try:
self.plugin = self.get_option('plugin')
configuration = stackl_client.Configuration()
configuration.host = self.get_option("host")
api_client = stackl_client.ApiClient(configuration=configuration)
api_instance = stackl_client.StackInstancesApi(api_client=api_client)
stack_instance_name = self.get_option("stack_instance")
stack_instance = api_instance.get_stack_instance(stack_instance_name)
for service, si_service in stack_instance.services.items():
self.inventory.add_group(service)
self.inventory.set_variable(service, "infrastructure_target",
si_service.infrastructure_target)
self.inventory.set_variable(service, "provisioning_parameters",
si_service.provisioning_parameters)
if si_service.hosts is not None:
for host in si_service.hosts:
self.inventory.add_host(host=host, group=service)
self.inventory.set_variable(service, "ansible_user", si_service.connection_credentials.username)
self.inventory.set_variable(service, "ansible_password", si_service.connection_credentials.password)
self.inventory.set_variable(service, "ansible_become_password", si_service.connection_credentials.password)
self.inventory.set_variable(service, "ansible_ssh_common_args", "-o StrictHostKeyChecking=no")
else:
self.inventory.add_host(host="kubernetes-" + service, group=service)
except Exception as e:
raise AnsibleParserError(
'All correct options required: {}'.format(e))