mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Add a custom regex_match filter to the vmware_inventory script (#21488)
This commit is contained in:
parent
47887a2edd
commit
447bb2b150
1 changed files with 16 additions and 2 deletions
|
@ -27,8 +27,8 @@ import argparse
|
||||||
import atexit
|
import atexit
|
||||||
import datetime
|
import datetime
|
||||||
import getpass
|
import getpass
|
||||||
import jinja2
|
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import six
|
import six
|
||||||
import ssl
|
import ssl
|
||||||
import sys
|
import sys
|
||||||
|
@ -37,6 +37,7 @@ import uuid
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from six.moves import configparser
|
from six.moves import configparser
|
||||||
from time import time
|
from time import time
|
||||||
|
from jinja2 import Environment
|
||||||
|
|
||||||
HAS_PYVMOMI = False
|
HAS_PYVMOMI = False
|
||||||
try:
|
try:
|
||||||
|
@ -61,6 +62,15 @@ except ImportError:
|
||||||
pass
|
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):
|
class VMwareMissingHostException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -104,6 +114,10 @@ class VMWareInventory(object):
|
||||||
|
|
||||||
custom_fields = {}
|
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
|
# translation table for attributes to fetch for known vim types
|
||||||
if not HAS_PYVMOMI:
|
if not HAS_PYVMOMI:
|
||||||
vimTable = {}
|
vimTable = {}
|
||||||
|
@ -498,7 +512,7 @@ class VMWareInventory(object):
|
||||||
|
|
||||||
mapping = {}
|
mapping = {}
|
||||||
for k, v in inventory['_meta']['hostvars'].items():
|
for k, v in inventory['_meta']['hostvars'].items():
|
||||||
t = jinja2.Template(pattern)
|
t = self.env.from_string(pattern)
|
||||||
newkey = None
|
newkey = None
|
||||||
try:
|
try:
|
||||||
newkey = t.render(v)
|
newkey = t.render(v)
|
||||||
|
|
Loading…
Reference in a new issue