forked from ansible/ansible
/
consul_facts
executable file
·149 lines (128 loc) · 4.24 KB
/
consul_facts
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python
# -*- coding: utf-8 -*-
# -*- mode: python -*-
DOCUMENTATION = '''
---
module: consul_facts
author: Matthew Finlayson
short_description: Gather facts from consul
version_added: 1.6.11
requirements: [ consulate ]
description:
- Gather facts from consul for kv, nodes, and services
options:
host:
required: false
default: "localhost"
description:
- Optionally specify a consul host to address.
port:
required: false
default: "8500"
description:
- Optionally specify a consul port to address.
dc:
required: false
default: "None"
description:
- Optionally specify a datacenter to address.
'''
EXAMPLES = '''
- name: Gather info from consul
hosts: localhost
gather_facts: False
tasks:
- name: Get facts from consul
local_action:
module: consul_facts
host: consul.dev.amazon.com
'''
import consulate
class Consul(object):
def __init__(self, module):
self.module = module
self.host = module.params['host']
self.port = module.params['port']
self.dc = module.params['dc']
self.session = consulate.Consulate(host=self.host, port=self.port, dc=self.dc)
self.params = {
'host': self.host,
'port': self.port,
'dc': self.dc
}
def consul_facts(self):
facts = {'kv': self.get_keys(), 'consul': self.host, 'nodes': self.get_nodes(),
'services': self.get_services()}
self.module.exit_json(changed=False, ansible_facts={'consul_facts': facts})
def get_keys(self):
root = {}
items = self.session.kv.items()
for key, value in items.iteritems():
self._items_to_dict(root, key, value)
return root
def _items_to_dict(self, root, key, value):
if not '/' in key:
if value is None:
if isinstance(root, dict):
root = []
root.extend([key])
else:
root[key] = value
return root
else:
atoms = key.split('/')
if value is None and len(atoms) == 2:
if not atoms[0] in root:
root[atoms[0]] = []
if isinstance(root, dict) and not root.has_key(atoms[0]):
root[atoms[0]] = {}
return self._items_to_dict(root[atoms[0]], "/".join(atoms[1:len(atoms)]), value)
def get_roles(self):
output = {}
for keys in self.session.kv.find('/roles/', '/'):
for it in self.session.kv.find(keys, '/'):
value = self.session.kv[it]
if value is None:
output[it] = []
for _list in self.session.kv.find(it, '/'):
if it != _list:
sanitized_value = _list.replace(it, '')
if sanitized_value.endswith('/'):
sanitized_value = sanitized_value[:-1]
output[it].append(sanitized_value)
else:
output[it] = self.session.kv[it]
return output
def get_nodes(self):
output = {}
for node in self.session.catalog.nodes():
output[node['Node']] = self.session.catalog.node(node['Node'])
return output
def get_services(self):
output = {}
for service in self.session.catalog.services():
for service_name in service.iterkeys():
output[service_name] = self.session.catalog.service(service_name)
return output
def main():
consul_defaults = {
'host': 'localhost',
'port': '8500',
'dc': None
}
module = AnsibleModule(
argument_spec=dict(
host=dict(default=consul_defaults['host'], type='str'),
port=dict(default=consul_defaults['port'], type='str'),
dc=dict(default=consul_defaults['dc'], type='str')
),
bypass_checks=True,
check_invalid_arguments=False,
supports_check_mode=False
)
consul = Consul(module)
consul.consul_facts()
# import module snippets
from ansible.module_utils.basic import *
# ## invoke the module
main()