/
ara_record.py
196 lines (172 loc) · 6 KB
/
ara_record.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
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
# Copyright (c) 2017 Red Hat, Inc.
#
# This file is part of ARA: Ansible Run Analysis.
#
# ARA is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ARA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
from ansible.plugins.action import ActionBase
try:
from ara.api.records import RecordApi
from ara.webapp import create_app
from flask import current_app
HAS_ARA = True
except ImportError:
HAS_ARA = False
DOCUMENTATION = """
---
module: ara_record
short_description: Ansible module to record persistent data with ARA.
version_added: "2.0"
author: "RDO Community <rdo-list@redhat.com>"
description:
- Ansible module to record persistent data with ARA.
options:
playbook:
description:
- uuid of the playbook to write the key to
required: false
version_added: 0.13.2
key:
description:
- Name of the key to write data to
required: true
value:
description:
- Value of the key written to
required: true
type:
description:
- Type of the key
choices: [text, url, json, list, dict]
default: text
requirements:
- "python >= 2.6"
- "ara >= 0.10.0"
"""
EXAMPLES = """
# Write static data
- ara_record:
key: "foo"
value: "bar"
# Write data to a specific (previously run) playbook
# (Retrieve playbook uuid's with 'ara playbook list')
- ara_record:
playbook: uuuu-iiii-dddd-0000
key: logs
value: "{{ lookup('file', '/var/log/ansible.log') }}"
type: text
# Write dynamic data
- shell: cd dev && git rev-parse HEAD
register: git_version
delegate_to: localhost
# Registering the result of an ara_record task is equivalent to doing an
# ara_read on the key
- ara_record:
key: "git_version"
value: "{{ git_version.stdout }}"
register: version
- name: Print recorded data
debug:
msg: "{{ version.playbook_id}} - {{ version.key }}: {{ version.value }}
# Write data with a type (otherwise defaults to "text")
# This changes the behavior on how the value is presented in the web interface
- ara_record:
key: "{{ item.key }}"
value: "{{ item.value }}"
type: "{{ item.type }}"
with_items:
- { key: "log", value: "error", type: "text" }
- { key: "website", value: "http://domain.tld", type: "url" }
- { key: "data", value: "{ 'key': 'value' }", type: "json" }
- { key: "somelist", value: ['one', 'two'], type: "list" }
- { key: "somedict", value: {'key': 'value' }, type: "dict" }
"""
class ActionModule(ActionBase):
""" Record persistent data as key/value pairs in ARA """
TRANSFERS_FILES = False
VALID_ARGS = frozenset(('playbook', 'key', 'value', 'type'))
VALID_TYPES = ['text', 'url', 'json', 'list', 'dict']
def create_or_update_key(self, playbook_id, key, value, type):
resp, exists = RecordApi().get(
playbook_id=playbook_id,
key=key
)
if resp.status_code == 404:
# TODO: Do a better job at validating this
resp, data = RecordApi().post(
playbook_id=playbook_id,
key=key,
value=value,
type=type
)
else:
# TODO: Do a better job at validating this
resp, data = RecordApi().patch(
id=exists[0]['id'],
key=key,
value=value,
type=type
)
return data
def run(self, tmp=None, task_vars=None):
if task_vars is None:
task_vars = dict()
if not HAS_ARA:
result = {
'failed': True,
'msg': 'ARA is required to run this module.'
}
return result
app = create_app()
if not current_app:
context = app.app_context()
context.push()
for arg in self._task.args:
if arg not in self.VALID_ARGS:
result = {
"failed": True,
"msg": '{0} is not a valid option.'.format(arg)
}
return result
result = super(ActionModule, self).run(tmp, task_vars)
playbook_id = self._task.args.get('playbook', None)
key = self._task.args.get('key', None)
value = self._task.args.get('value', None)
type = self._task.args.get('type', 'text')
required = ['key', 'value']
for parameter in required:
if not self._task.args.get(parameter):
result['failed'] = True
result['msg'] = "Parameter '{0}' is required".format(parameter)
return result
if type not in self.VALID_TYPES:
result['failed'] = True
msg = "Type '{0}' is not supported, choose one of: {1}".format(
type, ", ".join(self.VALID_TYPES)
)
result['msg'] = msg
return result
if playbook_id is None:
# Retrieve playbook_id from the cached context
playbook_id = current_app._cache['playbook']['id']
try:
data = self.create_or_update_key(playbook_id, key, value, type)
result['key'] = data['key']
result['value'] = data['value']
result['type'] = data['type']
result['playbook_id'] = data['playbook']['id']
result['msg'] = 'Data recorded in ARA for this playbook.'
except Exception as e:
result['failed'] = True
result['msg'] = 'Data not recorded in ARA: {0}'.format(str(e))
return result