Add tags filter to linode inventory plugin (#1551)

* Add tags filter to linode inventory plugin

* add tags to return tuple on line 66 in test_linode.py

* Add period in changelog fragment.
use if any() rather than for ... if list completion

* Clarify the description of the ``tags`` option

* Updated description to remove syntax error of line break.
This commit is contained in:
cdbunch72 2021-01-17 05:29:20 -06:00 committed by GitHub
parent b31583b441
commit 3b9c6d496b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 7 deletions

View file

@ -35,6 +35,12 @@ DOCUMENTATION = r'''
default: []
type: list
required: false
tags:
description: Populate inventory only with instances which have at least one of the tags listed here.
default: []
type: list
reqired: false
version_added: 2.0.0
types:
description: Populate inventory with instances with this type.
default: []
@ -135,7 +141,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
for linode_group in self.linode_groups:
self.inventory.add_group(linode_group)
def _filter_by_config(self, regions, types):
def _filter_by_config(self, regions, types, tags):
"""Filter instances by user specified configuration."""
if regions:
self.instances = [
@ -149,6 +155,12 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
if instance.type.id in types
]
if tags:
self.instances = [
instance for instance in self.instances
if any(tag in instance.tags for tag in tags)
]
def _add_instances_to_groups(self):
"""Add instance names to their dynamic inventory groups."""
for instance in self.instances:
@ -193,6 +205,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
'type_to_be': list,
'value': config_data.get('types', [])
},
'tags': {
'type_to_be': list,
'value': config_data.get('tags', [])
},
}
for name in options:
@ -204,8 +220,9 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
regions = options['regions']['value']
types = options['types']['value']
tags = options['tags']['value']
return regions, types
return regions, types, tags
def verify_file(self, path):
"""Verify the Linode configuration file."""
@ -228,8 +245,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
self._get_instances_inventory()
strict = self.get_option('strict')
regions, types = self._get_query_options(config_data)
self._filter_by_config(regions, types)
regions, types, tags = self._get_query_options(config_data)
self._filter_by_config(regions, types, tags)
self._add_groups()
self._add_instances_to_groups()