diff --git a/changelogs/fragments/1549-add-tag-filter-to-linode-inventory.yml b/changelogs/fragments/1549-add-tag-filter-to-linode-inventory.yml new file mode 100644 index 0000000000..4e11bf2463 --- /dev/null +++ b/changelogs/fragments/1549-add-tag-filter-to-linode-inventory.yml @@ -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). diff --git a/plugins/inventory/linode.py b/plugins/inventory/linode.py index 617f767e1f..327b58ca8e 100644 --- a/plugins/inventory/linode.py +++ b/plugins/inventory/linode.py @@ -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() diff --git a/tests/unit/plugins/inventory/test_linode.py b/tests/unit/plugins/inventory/test_linode.py index bbab0b9adb..427a7c69b3 100644 --- a/tests/unit/plugins/inventory/test_linode.py +++ b/tests/unit/plugins/inventory/test_linode.py @@ -58,18 +58,20 @@ def test_validation_option_bad_option(inventory): def test_empty_config_query_options(inventory): - regions, types = inventory._get_query_options({}) - assert regions == types == [] + regions, types, tags = inventory._get_query_options({}) + assert regions == types == tags == [] def test_conig_query_options(inventory): - regions, types = inventory._get_query_options({ + regions, types, tags = inventory._get_query_options({ 'regions': ['eu-west', 'us-east'], 'types': ['g5-standard-2', 'g6-standard-2'], + 'tags': ['web-server'], }) assert regions == ['eu-west', 'us-east'] assert types == ['g5-standard-2', 'g6-standard-2'] + assert tags == ['web-server'] def test_verify_file_bad_config(inventory):