diff options
author | Yujun Zhang <zhang.yujunz@zte.com.cn> | 2017-04-06 17:18:54 +0800 |
---|---|---|
committer | Yujun Zhang <zhang.yujunz@zte.com.cn> | 2017-04-06 18:07:58 +0800 |
commit | c6c68d91a08dc9315da8c209e2046e9a0300c997 (patch) | |
tree | f6ad57835f5ec57a4a6a94786e6c95217a2859af /qtip/ansible_library/plugins/action/collect.py | |
parent | 62460407a8fbcc99c154df3eabb8732c1908992e (diff) |
Add ansible action plugin `collect`
- this plugin collects information or metrics from string
- it is reworked from qtip.collector.parser.grep
- the captured subgroups are always list even only one match found
Change-Id: I1def3d7b40c7928b503fae1be531976a13e5d0be
Signed-off-by: Yujun Zhang <zhang.yujunz@zte.com.cn>
Diffstat (limited to 'qtip/ansible_library/plugins/action/collect.py')
-rw-r--r-- | qtip/ansible_library/plugins/action/collect.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/qtip/ansible_library/plugins/action/collect.py b/qtip/ansible_library/plugins/action/collect.py new file mode 100644 index 00000000..88ad0e35 --- /dev/null +++ b/qtip/ansible_library/plugins/action/collect.py @@ -0,0 +1,45 @@ +#!/usr/bin/python + +############################################################### +# Copyright (c) 2017 ZTE Corporation +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from collections import defaultdict +import re + +from ansible.plugins.action import ActionBase + + +class ActionModule(ActionBase): + + def run(self, tmp=None, task_vars=None): + result = super(ActionModule, self).run(tmp, task_vars) + + if result.get('skipped', False): + return result + + string = self._task.args.get('string') + patterns = self._task.args.get('patterns') + + return collect(patterns, string) + + +def collect(patterns, string): + """collect all named subgroups of the match into a list keyed by subgroup name + """ + captured = defaultdict(list) + + if not isinstance(patterns, list): + patterns = [patterns] + + for p in patterns: + for match_obj in re.finditer(p, string, re.MULTILINE): + for (key, value) in match_obj.groupdict().items(): + captured[key].append(value) + + return captured |