-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathansible_hacking.py
67 lines (54 loc) · 2.21 KB
/
ansible_hacking.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
#!/usr/bin/env python
"""
Copyright (c) 2017 World Wide Technology, Inc.
All rights reserved.
module: ansible_hacking.py
author: Joel W. King, (@joelwking) World Wide Technology
short_description: A mock object to simulate the Ansible environment for remote debugging
reference: http://docs.ansible.com/ansible/dev_guide/developing_modules_general.html
description: This code provides an alternative to using the Ansible hacking/test-module
"""
import sys
import json
class AnsibleModule(object):
""" Mock class for testing Ansible modules outside of the Ansible framework.
"""
INPUT_JSON = "ansible_hacking.json"
NO = ("n","N", "no", "No", "NO", "False", "FALSE", "false", "off", "Off", "OFF")
YES = ("y", "Y", "yes", "Yes", "YES", "True", "TRUE", "true", "on", "On", "ON")
def __init__(self, **kwargs):
""" Ansible internally saves arguments to an arguments file,
we read from our own JSON file.
"""
print "Entered ansible_hacking, AnsibleModule \n%s" % (json.dumps(kwargs, indent=4))
self.params = self.read_params(AnsibleModule.INPUT_JSON)
print "params:\n%s\nExiting AnsibleModule __init__" % (json.dumps(self.params, indent=4))
def read_params(self, fname):
""" Arguments for testing are read from JSON format file.
"""
try:
jsonfile = open(fname, 'r')
except IOError:
print "input file: %s not found!" % fname
sys.exit(1)
infile = jsonfile.read()
jsonfile.close()
# BOOLEAN logic
params = json.loads(infile)
for key, value in params.items():
if value in AnsibleModule.NO:
params[key] = False
if value in AnsibleModule.YES:
params[key] = True
return params
def exit_json(self, **kwargs):
""" Modules return information to Ansible by printing a JSON string
to stdout before exiting.
"""
print "%s" % (json.dumps(kwargs, indent=4))
sys.exit(0)
def fail_json(self, msg):
"""Fail with a message formatted in JSON.
"""
print json.dumps({'msg': msg}, indent=4)
sys.exit(1)