aboutsummaryrefslogtreecommitdiffstats
path: root/qtip/ansible_library
diff options
context:
space:
mode:
authorYujun Zhang <zhang.yujunz@zte.com.cn>2017-04-06 17:18:54 +0800
committerYujun Zhang <zhang.yujunz@zte.com.cn>2017-04-06 18:07:58 +0800
commitc6c68d91a08dc9315da8c209e2046e9a0300c997 (patch)
treef6ad57835f5ec57a4a6a94786e6c95217a2859af /qtip/ansible_library
parent62460407a8fbcc99c154df3eabb8732c1908992e (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')
-rw-r--r--qtip/ansible_library/plugins/__init__.py0
-rw-r--r--qtip/ansible_library/plugins/action/__init__.py0
-rw-r--r--qtip/ansible_library/plugins/action/collect.py45
3 files changed, 45 insertions, 0 deletions
diff --git a/qtip/ansible_library/plugins/__init__.py b/qtip/ansible_library/plugins/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/qtip/ansible_library/plugins/__init__.py
diff --git a/qtip/ansible_library/plugins/action/__init__.py b/qtip/ansible_library/plugins/action/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/qtip/ansible_library/plugins/action/__init__.py
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