-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathout
executable file
·455 lines (420 loc) · 15.7 KB
/
out
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
Concourse out resource
"""
# Python 2 and 3 compatibility
from __future__ import unicode_literals, print_function
import sys
import os
import time
from collections import namedtuple, defaultdict
from tempfile import NamedTemporaryFile
try:
# Python 3.x
from io import StringIO
except ImportError:
# Python 2.x
from StringIO import StringIO
from resource import Resource
# Future: from ansible.inventory.manager import InventoryManager
# from ansible.executor.stats import AggregateStats
from ansible.inventory import Inventory
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.errors import AnsibleError
from ansible.utils.vars import load_extra_vars, load_options_vars
from ansible.cli import CLI
from ansible.utils.display import Display
display = Display()
class PlaybookCLI(CLI):
DEFAULTS = {
'subset': None,
'ask_pass': False,
'ask_vault_pass': False,
'become': False,
'become_ask_pass': False,
'become_user': 'root',
'become_method': 'sudo',
'become_pass': None,
'check': False,
'connection': 'smart',
'diff': False,
'extra_vars': [],
'flush_cache': False,
'force_handlers': False,
'forks': 5,
'inventory': None,
'listhosts': False,
'listtags': False,
'listtasks': False,
'module_path': None,
'new_vault_password_file': None,
'output_file': None,
'private_key_file': None,
'remote_user': 'root',
'remote_pass': None,
'scp_extra_args': '',
'sftp_extra_args': '',
'skip_tags': [],
'ssh_common_args': '',
'ssh_extra_args': '',
'start_at_task': None,
'step': None,
'syntax': False, # None
'tags': ['all'],
'timeout': 10,
'vault_password': None,
'vault_password_file': None,
'verbosity': 0,
}
def __init__(self, config, logger):
super(self.__class__, self).__init__({})
self.logger = logger
options = dict(self.DEFAULTS)
options.update(config)
#del options_all['private_key_file']
#del options_all['module_path']
# Convert dicitionary to namedtuple 'Options'
self.options = namedtuple('Options', options.keys())(**options)
display.verbosity = self.options.verbosity
def parse(self):
pass
def run(self):
rcode = 0
playbook_path = self.options.playbook
inventory_path = self.options.inventory
verbose = self.options.verbosity
vault_password = self.options.vault_password
become_password = self.options.become_pass
remote_password = self.options.remote_pass
extra_vars = self.options.extra_vars
host_vars = None
group_vars = None
passwords = {}
# Set global verbosity
PlaybookExecutor.verbosity = verbose
super(self.__class__, self).run()
# Gets data from YAML/JSON files
loader = DataLoader()
if vault_password:
loader.set_vault_password(vault_password)
# Ansible 2.4
#
#inventory = InventoryManager(loader, inventory_path)
#variable_manager = VariableManager(loader=loader, inventory=inventory)
#
# Ansible 2.3.x
variable_manager = VariableManager()
if extra_vars:
variable_manager.extra_vars = load_extra_vars(loader=loader, options=self.options)
variable_manager.options_vars = load_options_vars(self.options)
if host_vars:
variable_manager.host_vars_files = host_vars
if group_vars:
variable_manager.group_vars_files = group_vars
inventory = Inventory(loader, variable_manager, inventory_path)
variable_manager.set_inventory(inventory)
# End Ansible
if become_password is not None:
passwords['become_pass'] = become_password
if remote_password is not None:
passwords['conn_pass'] = remote_password
self.logger.info("Running playbook '%s': %s" % (playbook_path, self.options))
# flush fact cache if requested
if self.options.flush_cache:
for host in inventory.list_hosts():
hostname = host.get_name()
variable_manager.clear_facts(hostname)
# Setup playbook executor, but don't run until run() called
playbook = PlaybookExecutor(
playbooks = [playbook_path],
inventory = inventory,
variable_manager = variable_manager,
loader = loader,
options = self.options,
passwords = passwords
)
# Results of PlaybookExecutor
stdout = ""
try:
stdout = playbook.run()
except AnsibleError as e:
msg = "Error running playbook '%s': %s" % (playbook_path, str(e))
self.logger.error(msg)
rcode = 1
else:
self.logger.info("Done '%s'" % (playbook_path))
stats = playbook._tqm._stats
return rcode, stdout, stats
class AnsiblePlaybook(Resource):
"""Concourse resource implementation for ansible-playbook"""
SOURCE = {
# private_key
"private_key_file": str,
"remote_user": str,
"remote_pass": str,
"vault_password": str,
"extra_vars": dict,
"inventory": dict,
"become": bool,
"become_method": str,
"become_user": str,
"become_pass": str,
"ssh_common_args": str,
"forks": int,
"tags": list,
"skip_tags": list,
}
PARAMS = {
# playbook
"src": str,
"playbook": str,
"extra_vars": dict,
"inventory": dict,
"become": bool,
"become_method": str,
"become_user": str,
"connection": str,
"timeout": int,
"ssh_common_args": str,
"verbosity": int,
"force_handlers": bool,
"forks": int,
"tags": list,
"skip_tags": list,
}
DEFAULT_INVENTORY_FILE="inventory.ini"
DEFAULT_INVENTORY_PATH="inventory"
def __init__(self, *args, **kwargs):
super(self.__class__, self).__init__(*args, **kwargs)
def _get_config_param(self, data, params={}):
config = {}
for p in params.keys():
value = data.get(p)
if value:
value_type = params[p]
if value_type is bool:
config[p] = str(value).lower() in ["true", "1", "yes", "y"]
elif value_type is None:
config[p] = value
else:
try:
config[p] = value_type(value)
except ValueError as e:
msg = "Cannot get config param '%s': %s" % (p, str(e))
self.logger.error(msg)
return config
def _hosts_group(self, name, data_group, output):
"""It Processes a inventory group. It can be a host (string),
a list of hosts (list of strings) or a dictionary.
"""
print("[%s]" % name, file=output)
if isinstance(data_group, dict):
if "hosts" in data_group:
for host in data_group['hosts']:
if isinstance(host, list):
print(' '.join(host), file=output)
else:
print(host, file=output)
print('', file=output)
if "vars" in data_group:
print("[%s:vars]" % name, file=output)
variables = data_group['vars']
try:
for v in variables.keys():
print("%s='%s'" % (v, variables[v]), file=output)
print('', file=output)
except Exception as e:
msg = "Inventory vars exception %s: '%s'" % (name, str(e))
self.logger.error(msg)
print('', file=output)
if "children" in data_group:
print("[%s:children]" % name, file=output)
for children in data_group['children']:
print(children, file=output)
print('', file=output)
elif isinstance(data_group, list):
for host in data_group:
if isinstance(host, list):
print(' '.join(host), file=output)
else:
print(host, file=output)
print('', file=output)
else:
print(data_group, file=output)
print('', file=output)
print('', file=output)
def hosts(self, data, path, hosts_file):
output = StringIO()
if isinstance(data, dict):
# inventory is (json) dictionary as specified in
# http://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html
self.logger.debug("Processing json inventory: '%s'" % repr(data))
groups_children = [
g for g in data.keys()
if isinstance(data[g],dict) and 'children' in data[g]
]
groups_leaf = [
g for g in data.keys()
if g not in groups_children
]
for group in groups_children:
self._hosts_group(group, data[group], output)
for group in groups_leaf:
self._hosts_group(group, data[group], output)
elif isinstance(data, list):
# inventory is a list of hosts
self.logger.debug("Processing list inventory: '%s'" % repr(data))
for host in data:
if isinstance(host, list):
print(' '.join(host), file=output)
else:
print(host, file=output)
else:
# inventory is something like "localhost"
msg = "Processing simple str inventory: '%s'" % repr(data)
self.logger.debug(msg)
print(data, file=output)
content = output.getvalue()
inventory_path = os.path.join(path, hosts_file)
try:
with open(inventory_path, 'w') as f:
f.write(content)
except Exception as e:
msg = "Cannot write inventory '%s': %s" % (inventory_path, str(e))
self.logger.error(msg)
raise
else:
output.close()
return content
def inventory(self, workfolder, source, params):
output = None
inventory = source.get('inventory', {})
# Fixme! avoid overwriting
inventory.update(params.get("inventory", {}))
hosts = inventory.get("hosts")
inventory_path = inventory.get("path", self.DEFAULT_INVENTORY_PATH)
inventory_file = inventory.get("file")
inventory_exec = inventory.get("executable")
# TODO
group_vars = inventory.get("group_vars")
host_vars = inventory.get("host_vars")
if inventory_exec:
# dynamic inventory executable
output = inventory_exec
else:
inventory_path = os.path.join(workfolder, inventory_path)
if not os.path.exists(inventory_path):
try:
os.makedirs(inventory_path)
except Exception as e:
msg = "Cannot create inventory folder '%s': %s" % (inventory_path, str(e))
self.logger.error(msg)
raise
if inventory_file:
# Inventory is pointing directly to the ini file
output = os.path.join(inventory_path, inventory_file)
else:
# Inventory is the full path
# Just create a inventory file
inventory_file = self.DEFAULT_INVENTORY_FILE
output = inventory_path
if hosts:
self.hosts(hosts, inventory_path, inventory_file)
return output
def configure(self, workfolder, source, params):
config = self._get_config_param(source, self.SOURCE)
private_key_path = config.get("private_key_file")
config_params = self._get_config_param(params, self.PARAMS)
config.update(config_params)
# Path
build_path = config_params.get("src", "src")
build_path = os.path.join(workfolder, build_path)
# Extra vars (just a dictionary)
extra_vars = config.get("extra_vars", {})
extra_vars.update(source.get("extra_vars", {}))
config['extra_vars'] = [extra_vars]
# Private key
private_key = source.get("private_key")
if private_key:
try:
tmp_file = NamedTemporaryFile(delete=False, suffix='.key')
private_key_path = tmp_file.name
with open(private_key_path, 'w') as f:
f.write(private_key)
except Exception as e:
msg = "Cannot create private key file: %s" % (str(e))
self.logger.error(msg)
raise
config['private_key_file'] = private_key_path
elif private_key_path:
config['private_key_file'] = os.path.join(build_path, private_key_path)
# Inventory
config['inventory'] = self.inventory(build_path, source, params)
# Playbook path
playbook_path = params.get("playbook", "playbook.yml")
playbook_path = os.path.join(build_path, playbook_path)
if not os.path.isfile(playbook_path):
msg = "Cannot find playbook file '%s'" % (playbook_path)
self.logger.error(msg)
raise ValueError(msg)
config['playbook'] = playbook_path
return config
def summarize(self, stats):
failed = []
unreachable = []
hosts = stats.processed.keys()
for h in hosts:
s = stats.summarize(h)
if s["failures"] > 0:
failed.append(h)
if s["unreachable"] > 0:
unreachable.append(h)
result = {
"hosts_all": hosts,
"hosts_failed": failed,
"hosts_unreachable": unreachable,
"processed": stats.processed,
"failures": stats.failures,
"ok": stats.ok,
"dark": stats.dark,
"changed": stats.changed,
"skipped": stats.skipped
}
self.logger.info("Playbook summary: %s" % (result))
return result
def metadata(self, rcode, result):
if rcode == 0:
statuscode = 0
if len(result.get("hosts_failed", [])) > 0:
statuscode = 2
if len(result.get("hosts_unreachable", [])) > 0:
statuscode = 3
else:
statuscode = rcode
metadata = []
for k in result.keys():
metadata.append({"name": str(k), "value": str(result[k]) })
metadata.append({"name": "statuscode", "value": str(statuscode) })
return statuscode, metadata
def update(self, folder, source, params):
config = self.configure(folder, source, params)
exitcode, stdout, stats = PlaybookCLI(config, self.logger).run()
result = self.summarize(stats)
rcode, metadata = self.metadata(exitcode, result)
timestamp = time.time()
version = { "timestamp": str(timestamp) }
rvalue = { "version": version, "metadata": metadata }
return rcode, rvalue
if __name__ == '__main__':
r = AnsiblePlaybook()
try:
rcode = r.run(os.path.basename(__file__))
except Exception as e:
raise
sys.stderr.write("ERROR: " + str(e) + "\n")
sys.exit(1)
sys.exit(rcode)