Add role information to task in zuul_json callback

In comparison to other callback plugins like the default (stdout)
plugin, the role information for an executed task is missing in the
json output. To be complient to the provided task output containing
the name and uuid field, we've defined a similar data structure
containing the role information. This will only be added to the result
set if the task contained the necessary role information.

Change-Id: I8d94ba077e0bc90b5cf6510804bbd57c38184a9d
This commit is contained in:
Benedikt Loeffler 2018-04-10 13:51:17 +02:00
parent 1e3e5d33fb
commit 2dd4c5274b
5 changed files with 32 additions and 1 deletions

View File

@ -0,0 +1,6 @@
---
features:
- |
Add role name to json log
The json log contains now also the role name and the uuid
similar to the task entry.

View File

@ -0,0 +1,3 @@
- hosts: all
roles:
- json-role

View File

@ -0,0 +1,3 @@
- name: Print role name in json
debug:
msg: Test for role name

View File

@ -81,6 +81,19 @@ class TestZuulJSON(AnsibleZuulTestCase):
self.assertIn('rosebud', text)
self.assertNotIn('setec', text)
def test_json_role_log(self):
job = self._run_job('json-role')
with self.jobLog(job):
build = self.history[-1]
self.assertEqual(build.result, 'SUCCESS')
text = self._get_json_as_text(build)
self.assertIn('json-role', text)
json_result = json.loads(text)
role_name = json_result[0]['plays'][0]['tasks'][0]['role']['name']
self.assertEqual('json-role', role_name)
def test_json_time_log(self):
job = self._run_job('no-log')
with self.jobLog(job):

View File

@ -102,7 +102,7 @@ class CallbackModule(CallbackBase):
}
def _new_task(self, task):
return {
data = {
'task': {
'name': task.name,
'id': str(task._uuid),
@ -112,6 +112,12 @@ class CallbackModule(CallbackBase):
},
'hosts': {}
}
if task._role:
data['role'] = {
'name': task._role.get_name(),
'id': str(task._role._uuid)
}
return data
def v2_playbook_on_start(self, playbook):
self._playbook_name = os.path.splitext(playbook._file_name)[0]