summaryrefslogtreecommitdiff
path: root/contrib/inventory/vmware_inventory.py
diff options
context:
space:
mode:
authorjctanner <tanner.jc@gmail.com>2017-03-17 21:25:00 -0400
committerGitHub <noreply@github.com>2017-03-17 21:25:00 -0400
commit447bb2b1504c04f7d91b6e735ab8c3aa931291c5 (patch)
tree690d565828359554ce5fd6664fb023299112f733 /contrib/inventory/vmware_inventory.py
parent47887a2eddf4b53ff1292c000b26852c306b727a (diff)
downloadansible-447bb2b1504c04f7d91b6e735ab8c3aa931291c5.tar.gz
Add a custom regex_match filter to the vmware_inventory script (#21488)
Diffstat (limited to 'contrib/inventory/vmware_inventory.py')
-rwxr-xr-xcontrib/inventory/vmware_inventory.py18
1 files 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)