Repository: ansible-collections/community.general

Issue:
Add a tags filter to linode inventory plugin


##### SUMMARY
Add tags option to filter linodes by tag

##### ISSUE TYPE
- Feature Idea

##### COMPONENT NAME
linode inventory plugin

##### ADDITIONAL INFORMATION
tags option would be a list of tags to include in the returned inventory
```
plugin: community.general.linode
tags:
  - web
  - mail

```





Discussion:
Files identified in the description:
* [`lib/ansible/plugins/inventory`](https://github.com/ansible-collections/community.general/blob/master/lib/ansible/plugins/inventory)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
!component lib/ansible/plugins/inventory/linode.py

Resolving patch:
diff --git a/plugins/inventory/linode.py b/plugins/inventory/linode.py
--- a/plugins/inventory/linode.py
+++ b/plugins/inventory/linode.py
@@ -36,6 +36,12 @@
           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: []
@@ -136,7 +142,7 @@ def _add_groups(self):
         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 = [
@@ -150,6 +156,12 @@ def _filter_by_config(self, regions, 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):
         """Add instance names to their dynamic inventory groups."""
         for instance in self.instances:
@@ -194,6 +206,10 @@ def _get_query_options(self, config_data):
                 'type_to_be': list,
                 'value': config_data.get('types', [])
             },
+            'tags': {
+                'type_to_be': list,
+                'value': config_data.get('tags', [])
+            },
         }
 
         for name in options:
@@ -205,8 +221,9 @@ def _get_query_options(self, config_data):
 
         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."""
@@ -229,8 +246,8 @@ def parse(self, inventory, loader, path, cache=True):
         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()


Test patch:
diff --git a/tests/unit/plugins/inventory/test_linode.py b/tests/unit/plugins/inventory/test_linode.py
--- 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):