diff --git a/awx/main/models/inventory.py b/awx/main/models/inventory.py index baf4624726..c8bc88eb0b 100644 --- a/awx/main/models/inventory.py +++ b/awx/main/models/inventory.py @@ -2044,9 +2044,25 @@ class azure_rm(PluginFileInjector): for key, loc in old_filterables: value = source_vars.get(key, None) if value and isinstance(value, str): - user_filters.append('{} not in {}'.format( - loc, value.split(',') - )) + # tags can be list of key:value pairs + # e.g. 'Creator:jmarshall, peanutbutter:jelly' + # or tags can be a list of keys + # e.g. 'Creator, peanutbutter' + if key == "tags": + # grab each key value pair + for kvpair in value.split(','): + # split into key and value + kv = kvpair.split(':') + # filter out any host that does not have key + # in their tags.keys() variable + user_filters.append('"{}" not in tags.keys()'.format(kv[0].strip())) + # if a value is provided, check that the key:value pair matches + if len(kv) > 1: + user_filters.append('tags["{}"] != "{}"'.format(kv[0].strip(), kv[1].strip())) + else: + user_filters.append('{} not in {}'.format( + loc, value.split(',') + )) if user_filters: ret.setdefault('exclude_host_filters', []) ret['exclude_host_filters'].extend(user_filters) diff --git a/awx/main/tests/data/inventory/plugins/azure_rm/files/azure_rm.yml b/awx/main/tests/data/inventory/plugins/azure_rm/files/azure_rm.yml index 9dfcae9561..4479e79058 100644 --- a/awx/main/tests/data/inventory/plugins/azure_rm/files/azure_rm.yml +++ b/awx/main/tests/data/inventory/plugins/azure_rm/files/azure_rm.yml @@ -3,6 +3,10 @@ conditional_groups: default_host_filters: [] exclude_host_filters: - resource_group not in ['foo_resources', 'bar_resources'] +- '"Creator" not in tags.keys()' +- tags["Creator"] != "jmarshall" +- '"peanutbutter" not in tags.keys()' +- tags["peanutbutter"] != "jelly" - location not in ['southcentralus', 'westus'] fail_on_template_errors: false hostvar_expressions: diff --git a/awx/main/tests/data/inventory/scripts/azure_rm/files/file_reference b/awx/main/tests/data/inventory/scripts/azure_rm/files/file_reference index 78f56e0e7a..e3410bff06 100644 --- a/awx/main/tests/data/inventory/scripts/azure_rm/files/file_reference +++ b/awx/main/tests/data/inventory/scripts/azure_rm/files/file_reference @@ -7,4 +7,5 @@ locations = southcentralus,westus base_source_var = value_of_var use_private_ip = True resource_groups = foo_resources,bar_resources +tags = Creator:jmarshall, peanutbutter:jelly diff --git a/awx/main/tests/functional/test_inventory_source_injectors.py b/awx/main/tests/functional/test_inventory_source_injectors.py index 729658a58d..c41e596edc 100644 --- a/awx/main/tests/functional/test_inventory_source_injectors.py +++ b/awx/main/tests/functional/test_inventory_source_injectors.py @@ -54,7 +54,8 @@ INI_TEST_VARS = { }, 'azure_rm': { 'use_private_ip': True, - 'resource_groups': 'foo_resources,bar_resources' + 'resource_groups': 'foo_resources,bar_resources', + 'tags': 'Creator:jmarshall, peanutbutter:jelly' }, 'satellite6': { 'satellite6_group_patterns': 'foo_group_patterns',