From 219a6a67f1e97236f3bef7957f67858140de2a69 Mon Sep 17 00:00:00 2001 From: jctanner Date: Wed, 27 Sep 2017 19:06:01 -0400 Subject: [PATCH] Add a select_chain_match filter to vmware_inventory (#28778) --- contrib/inventory/vmware_inventory.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/contrib/inventory/vmware_inventory.py b/contrib/inventory/vmware_inventory.py index b6ed76109ec..2fc0e09ffe2 100755 --- a/contrib/inventory/vmware_inventory.py +++ b/contrib/inventory/vmware_inventory.py @@ -41,6 +41,7 @@ from __future__ import print_function import atexit import datetime import getpass +import itertools import json import os import re @@ -84,6 +85,14 @@ def regex_match(s, pattern): return False +def select_chain_match(inlist, key, pattern): + '''Get a key from a list of dicts, squash values to a single list, then filter''' + outlist = [x[key] for x in inlist] + outlist = list(itertools.chain(*outlist)) + outlist = [x for x in outlist if regex_match(x, pattern)] + return outlist + + class VMwareMissingHostException(Exception): pass @@ -127,6 +136,7 @@ class VMWareInventory(object): # use jinja environments to allow for custom filters env = Environment() env.filters['regex_match'] = regex_match + env.filters['select_chain_match'] = select_chain_match # translation table for attributes to fetch for known vim types