From 447bb2b1504c04f7d91b6e735ab8c3aa931291c5 Mon Sep 17 00:00:00 2001 From: jctanner Date: Fri, 17 Mar 2017 21:25:00 -0400 Subject: [PATCH] Add a custom regex_match filter to the vmware_inventory script (#21488) --- contrib/inventory/vmware_inventory.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/contrib/inventory/vmware_inventory.py b/contrib/inventory/vmware_inventory.py index 58ee473dca..c175830883 100755 --- a/contrib/inventory/vmware_inventory.py +++ b/contrib/inventory/vmware_inventory.py @@ -27,8 +27,8 @@ import argparse import atexit import datetime import getpass -import jinja2 import os +import re import six import ssl import sys @@ -37,6 +37,7 @@ import uuid from collections import defaultdict from six.moves import configparser from time import time +from jinja2 import Environment HAS_PYVMOMI = False try: @@ -61,6 +62,15 @@ except ImportError: pass +def regex_match(s, pattern): + '''Custom filter for regex matching''' + reg = re.compile(pattern) + if reg.match(s): + return True + else: + return False + + class VMwareMissingHostException(Exception): pass @@ -104,6 +114,10 @@ class VMWareInventory(object): custom_fields = {} + # use jinja environments to allow for custom filters + env = Environment() + env.filters['regex_match'] = regex_match + # translation table for attributes to fetch for known vim types if not HAS_PYVMOMI: vimTable = {} @@ -498,7 +512,7 @@ class VMWareInventory(object): mapping = {} for k, v in inventory['_meta']['hostvars'].items(): - t = jinja2.Template(pattern) + t = self.env.from_string(pattern) newkey = None try: newkey = t.render(v)