1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add a select_chain_match filter to vmware_inventory (#28778)

This commit is contained in:
jctanner 2017-09-27 19:06:01 -04:00 committed by ansibot
parent 0dd3a61a75
commit 219a6a67f1

View file

@ -41,6 +41,7 @@ from __future__ import print_function
import atexit import atexit
import datetime import datetime
import getpass import getpass
import itertools
import json import json
import os import os
import re import re
@ -84,6 +85,14 @@ def regex_match(s, pattern):
return False 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): class VMwareMissingHostException(Exception):
pass pass
@ -127,6 +136,7 @@ class VMWareInventory(object):
# use jinja environments to allow for custom filters # use jinja environments to allow for custom filters
env = Environment() env = Environment()
env.filters['regex_match'] = regex_match env.filters['regex_match'] = regex_match
env.filters['select_chain_match'] = select_chain_match
# translation table for attributes to fetch for known vim types # translation table for attributes to fetch for known vim types