mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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:
parent
b31583b441
commit
3b9c6d496b
3 changed files with 30 additions and 7 deletions
|
@ -0,0 +1,4 @@
|
||||||
|
minor_changes:
|
||||||
|
- linode inventory plugin - add support for ``tags`` option to filter
|
||||||
|
instances by tag
|
||||||
|
(https://github.com/ansible-collections/community.general/issues/1549).
|
|
@ -35,6 +35,12 @@ DOCUMENTATION = r'''
|
||||||
default: []
|
default: []
|
||||||
type: list
|
type: list
|
||||||
required: false
|
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:
|
types:
|
||||||
description: Populate inventory with instances with this type.
|
description: Populate inventory with instances with this type.
|
||||||
default: []
|
default: []
|
||||||
|
@ -135,7 +141,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||||
for linode_group in self.linode_groups:
|
for linode_group in self.linode_groups:
|
||||||
self.inventory.add_group(linode_group)
|
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."""
|
"""Filter instances by user specified configuration."""
|
||||||
if regions:
|
if regions:
|
||||||
self.instances = [
|
self.instances = [
|
||||||
|
@ -149,6 +155,12 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||||
if instance.type.id in types
|
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):
|
def _add_instances_to_groups(self):
|
||||||
"""Add instance names to their dynamic inventory groups."""
|
"""Add instance names to their dynamic inventory groups."""
|
||||||
for instance in self.instances:
|
for instance in self.instances:
|
||||||
|
@ -193,6 +205,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||||
'type_to_be': list,
|
'type_to_be': list,
|
||||||
'value': config_data.get('types', [])
|
'value': config_data.get('types', [])
|
||||||
},
|
},
|
||||||
|
'tags': {
|
||||||
|
'type_to_be': list,
|
||||||
|
'value': config_data.get('tags', [])
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for name in options:
|
for name in options:
|
||||||
|
@ -204,8 +220,9 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||||
|
|
||||||
regions = options['regions']['value']
|
regions = options['regions']['value']
|
||||||
types = options['types']['value']
|
types = options['types']['value']
|
||||||
|
tags = options['tags']['value']
|
||||||
|
|
||||||
return regions, types
|
return regions, types, tags
|
||||||
|
|
||||||
def verify_file(self, path):
|
def verify_file(self, path):
|
||||||
"""Verify the Linode configuration file."""
|
"""Verify the Linode configuration file."""
|
||||||
|
@ -228,8 +245,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||||
self._get_instances_inventory()
|
self._get_instances_inventory()
|
||||||
|
|
||||||
strict = self.get_option('strict')
|
strict = self.get_option('strict')
|
||||||
regions, types = self._get_query_options(config_data)
|
regions, types, tags = self._get_query_options(config_data)
|
||||||
self._filter_by_config(regions, types)
|
self._filter_by_config(regions, types, tags)
|
||||||
|
|
||||||
self._add_groups()
|
self._add_groups()
|
||||||
self._add_instances_to_groups()
|
self._add_instances_to_groups()
|
||||||
|
|
|
@ -58,18 +58,20 @@ def test_validation_option_bad_option(inventory):
|
||||||
|
|
||||||
|
|
||||||
def test_empty_config_query_options(inventory):
|
def test_empty_config_query_options(inventory):
|
||||||
regions, types = inventory._get_query_options({})
|
regions, types, tags = inventory._get_query_options({})
|
||||||
assert regions == types == []
|
assert regions == types == tags == []
|
||||||
|
|
||||||
|
|
||||||
def test_conig_query_options(inventory):
|
def test_conig_query_options(inventory):
|
||||||
regions, types = inventory._get_query_options({
|
regions, types, tags = inventory._get_query_options({
|
||||||
'regions': ['eu-west', 'us-east'],
|
'regions': ['eu-west', 'us-east'],
|
||||||
'types': ['g5-standard-2', 'g6-standard-2'],
|
'types': ['g5-standard-2', 'g6-standard-2'],
|
||||||
|
'tags': ['web-server'],
|
||||||
})
|
})
|
||||||
|
|
||||||
assert regions == ['eu-west', 'us-east']
|
assert regions == ['eu-west', 'us-east']
|
||||||
assert types == ['g5-standard-2', 'g6-standard-2']
|
assert types == ['g5-standard-2', 'g6-standard-2']
|
||||||
|
assert tags == ['web-server']
|
||||||
|
|
||||||
|
|
||||||
def test_verify_file_bad_config(inventory):
|
def test_verify_file_bad_config(inventory):
|
||||||
|
|
Loading…
Reference in a new issue