Skip to content

Commit

Permalink
Merge pull request #341 from Simon-cto/AnsibleSetStatsFilterPlugin
Browse files Browse the repository at this point in the history
Dedicated log filter plugin to capture the output of ansible set_stats module
  • Loading branch information
ltamaster committed Oct 19, 2023
2 parents 7fd1cb2 + 6ff3077 commit 8bf314b
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ ext.pluginClassNames = [
'com.rundeck.plugins.ansible.plugin.AnsiblePlaybookInlineWorkflowStep',
'com.rundeck.plugins.ansible.plugin.AnsibleModuleWorkflowStep',
'com.rundeck.plugins.ansible.plugin.AnsiblePlaybookWorflowNodeStep',
'com.rundeck.plugins.ansible.plugin.AnsiblePlaybookInlineWorkflowNodeStep'
'com.rundeck.plugins.ansible.plugin.AnsiblePlaybookInlineWorkflowNodeStep',
'com.rundeck.plugins.ansible.logging.AnsibleSetStatsFilterPlugin'
].join(',')

apply plugin: 'java'
Expand Down Expand Up @@ -91,4 +92,4 @@ publishing {
from components.java
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

package com.rundeck.plugins.ansible.logging

import com.dtolabs.rundeck.core.execution.workflow.OutputContext
import com.dtolabs.rundeck.core.logging.LogEventControl
import com.dtolabs.rundeck.core.logging.LogLevel
import com.dtolabs.rundeck.core.logging.PluginLoggingContext
import com.dtolabs.rundeck.core.plugins.Plugin
import com.dtolabs.rundeck.core.plugins.configuration.PropertyValidator
import com.dtolabs.rundeck.core.plugins.configuration.ValidationException
import com.dtolabs.rundeck.plugins.descriptions.PluginDescription
import com.dtolabs.rundeck.plugins.descriptions.PluginProperty
import com.dtolabs.rundeck.plugins.descriptions.RenderingOption
import com.dtolabs.rundeck.plugins.descriptions.RenderingOptions
import com.dtolabs.rundeck.plugins.logging.LogFilterPlugin
import com.fasterxml.jackson.databind.ObjectMapper

import java.util.regex.Matcher
import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException

import java.util.Iterator
import com.google.gson.JsonParser
import com.google.gson.JsonObject
import com.google.gson.JsonElement


/**
* @author Simon Cateau
* @since 20/09/2023
*/
@Plugin(name = "ansible-set_stats", service = "LogFilter")
@PluginDescription(title = "Ansible set_stats",
description = '''Parses the output of the ansible set_stats module and generates the corresponding data context variables.\n\n
To display the output of the set_stats module, you must set show_custom_stats in section [defaults] in ansible.cfg or by defining environment variable ANSIBLE_SHOW_CUSTOM_STATS to true.
See the [official Ansible documentation](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set_stats_module.html).
''')

class AnsibleSetStatsFilterPlugin implements LogFilterPlugin{
@PluginProperty(
title = "Log Data",
description = "If true, log the captured data",
defaultValue = 'false'
)
Boolean outputData

Pattern setStatsGlobalPattern
OutputContext outputContext
Map<String, String> allData
ObjectMapper mapper

@Override
void init(final PluginLoggingContext context) {
String regex = /^\tRUN:\s(\{.*\})$/
setStatsGlobalPattern = Pattern.compile(regex)
outputContext = context.getOutputContext()
mapper = new ObjectMapper()
allData = [:]
}

@Override
void handleEvent(final PluginLoggingContext context, final LogEventControl event) {
if (event.eventType == 'log' && event.loglevel == LogLevel.NORMAL && event.message?.length() > 0) {
Matcher match = setStatsGlobalPattern.matcher(event.message)

if(match.matches()){
String jsonString = match.group(1)
JsonObject obj = JsonParser.parseString(jsonString).getAsJsonObject()
Iterator<String> keys = obj.keySet().iterator()
while(keys.hasNext()) {
String key = keys.next()
String value = obj.get(key).getAsString()
allData[key] = value
outputContext.addOutput("data", key, value)
}
}
}
}

@Override
void complete(final PluginLoggingContext context) {
if (allData) {
if (outputData) {
context.log(
2,
mapper.writeValueAsString(allData),
[
'content-data-type' : 'application/json',
'content-meta:table-title': 'Ansible set_stats: Results'
]
)
}
// allData = [:]
}
}
}

0 comments on commit 8bf314b

Please sign in to comment.