From 58a92876891a93b5a49f4d28a087183f039fe4ba Mon Sep 17 00:00:00 2001 From: cdbunch72 Date: Sun, 27 Dec 2020 06:53:41 -0600 Subject: [PATCH] =?UTF-8?q?Add=20support=20for=20keyed=5Fgroups,=20groups,?= =?UTF-8?q?=20and=20compose=20options=20to=20linode=20i=E2=80=A6=20(#1453)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add support for keyed_groups, groups, and compose options to linode inventory plugin * Changed instance,label to instance.label various lint fixes around spacing * Change self._strict to a local variable as it's no longer needed in other methods * Modified DESCRIPTION to add a version_added property to each of the new options. * Add Changelog fragment * Minor changes to the changelog fragment * Replace 'the standard' with 'support for' * Update description to note that the inventory groups are created by default from the group attribute not tags and that the group attribute has been depricated by Linode. * Remove trailing space in description * Add period to changelog fragment Co-authored-by: Clinton Bunch --- ...eyed_groups-to-linode-inventory-plugin.yml | 4 ++ plugins/inventory/linode.py | 49 +++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/1453-add-support-for-keyed_groups-to-linode-inventory-plugin.yml diff --git a/changelogs/fragments/1453-add-support-for-keyed_groups-to-linode-inventory-plugin.yml b/changelogs/fragments/1453-add-support-for-keyed_groups-to-linode-inventory-plugin.yml new file mode 100644 index 0000000000..d951992345 --- /dev/null +++ b/changelogs/fragments/1453-add-support-for-keyed_groups-to-linode-inventory-plugin.yml @@ -0,0 +1,4 @@ +minor_changes: + - linode inventory plugin - add support for ``keyed_groups``, ``groups``, + and ``compose`` options + (https://github.com/ansible-collections/community.general/issues/1326). diff --git a/plugins/inventory/linode.py b/plugins/inventory/linode.py index c308fb8213..ecaf86b4bc 100644 --- a/plugins/inventory/linode.py +++ b/plugins/inventory/linode.py @@ -17,7 +17,10 @@ DOCUMENTATION = r''' - Reads inventories from the Linode API v4. - Uses a YAML configuration file that ends with linode.(yml|yaml). - Linode labels are used by default as the hostnames. - - The inventory groups are built from groups and not tags. + - The default inventory groups are built from groups (deprecated by + Linode) and not tags. + extends_documentation_fragment: + - constructed options: plugin: description: marks this as an instance of the 'linode' plugin @@ -38,6 +41,14 @@ DOCUMENTATION = r''' default: [] type: list required: false + strict: + version_added: 2.0.0 + compose: + version_added: 2.0.0 + groups: + version_added: 2.0.0 + keyed_groups: + version_added: 2.0.0 ''' EXAMPLES = r''' @@ -51,13 +62,27 @@ regions: - eu-west types: - g5-standard-2 + +# Example with keyed_groups, groups, and compose +plugin: community.general.linode +access_token: foobar +keyed_groups: + - key: tags + separator: '' + - key: region + prefix: region +groups: + webservers: "'web' in (tags|list)" + mailservers: "'mail' in (tags|list)" +compose: + ansible_port: 2222 ''' import os from ansible.errors import AnsibleError, AnsibleParserError from ansible.module_utils.six import string_types -from ansible.plugins.inventory import BaseInventoryPlugin +from ansible.plugins.inventory import BaseInventoryPlugin, Constructable try: @@ -68,7 +93,7 @@ except ImportError: HAS_LINODE = False -class InventoryModule(BaseInventoryPlugin): +class InventoryModule(BaseInventoryPlugin, Constructable): NAME = 'community.general.linode' @@ -203,9 +228,27 @@ class InventoryModule(BaseInventoryPlugin): self._get_instances_inventory() + strict = self.get_option('strict') regions, types = self._get_query_options(config_data) self._filter_by_config(regions, types) self._add_groups() self._add_instances_to_groups() self._add_hostvars_for_instances() + for instance in self.instances: + variables = self.inventory.get_host(instance.label).get_vars() + self._add_host_to_composed_groups( + self.get_option('groups'), + variables, + instance.label, + strict=strict) + self._add_host_to_keyed_groups( + self.get_option('keyed_groups'), + variables, + instance.label, + strict=strict) + self._set_composite_vars( + self.get_option('compose'), + variables, + instance.label, + strict=strict)