summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/hrobot/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'ansible_collections/community/hrobot/plugins')
-rw-r--r--ansible_collections/community/hrobot/plugins/inventory/robot.py54
1 files changed, 32 insertions, 22 deletions
diff --git a/ansible_collections/community/hrobot/plugins/inventory/robot.py b/ansible_collections/community/hrobot/plugins/inventory/robot.py
index 409c90992..675be7f2c 100644
--- a/ansible_collections/community/hrobot/plugins/inventory/robot.py
+++ b/ansible_collections/community/hrobot/plugins/inventory/robot.py
@@ -25,6 +25,7 @@ DOCUMENTATION = r"""
- ansible.builtin.constructed
- ansible.builtin.inventory_cache
- community.hrobot.robot
+ - community.library_inventory_filtering_v1.inventory_filter
notes:
- The O(hetzner_user) and O(hetzner_password) options can be templated.
options:
@@ -43,12 +44,13 @@ DOCUMENTATION = r"""
- A dictionary of filter value pairs.
- Available filters are listed here are keys of server like C(status) or C(server_ip).
- See U(https://robot.your-server.de/doc/webservice/en.html#get-server) for all values that can be used.
- - This option has been renamed from O(filters) to O(simple_filters) in community.hrobot 1.9.0.
- The old name can still be used until community.hrobot 2.0.0.
+ - This option used to be called O(filters) before community.hrobot 2.0.0. It has been renamed from
+ O(filters) to O(simple_filters) in community.hrobotdns 1.9.0, and the old name was still available
+ as an alias until community.hrobot 2.0.0. O(filters) is now used for something else.
type: dict
default: {}
- aliases:
- - filters
+ filters:
+ version_added: 2.0.0
"""
EXAMPLES = r"""
@@ -58,12 +60,18 @@ plugin: community.hrobot.robot
filters:
status: ready
-# Example showing encrypted credentials
+# Example showing encrypted credentials and using filters
# (This assumes that Mozilla sops was used to encrypt keys/hetzner.sops.yaml, which contains two values
# hetzner_username and hetzner_password. Needs the community.sops collection to decode that file.)
plugin: community.hrobot.robot
hetzner_user: '{{ (lookup("community.sops.sops", "keys/hetzner.sops.yaml") | from_yaml).hetzner_username }}'
hetzner_password: '{{ (lookup("community.sops.sops", "keys/hetzner.sops.yaml") | from_yaml).hetzner_password }}'
+filters:
+ # Accept all servers in FSN1-DC1 and FSN1-DC2
+ - include: >-
+ hrobot_dc in ["FSN1-DC1", "FSN1-DC2"]
+ # Exclude all servers that didn't match any of the above filters
+ - exclude: true
# Example using constructed features to create groups
plugin: community.hrobot.robot
@@ -86,6 +94,8 @@ from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cachea
from ansible.template import Templar
from ansible.utils.display import Display
+from ansible_collections.community.library_inventory_filtering_v1.plugins.plugin_utils.inventory_filter import parse_filters, filter_host
+
from ansible_collections.community.hrobot.plugins.module_utils.robot import (
BASE_URL,
PluginException,
@@ -114,18 +124,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def parse(self, inventory, loader, path, cache=True):
super(InventoryModule, self).parse(inventory, loader, path)
servers = {}
- orig_config = self._read_config_data(path)
+ self._read_config_data(path)
self.load_cache_plugin()
cache_key = self.get_cache_key(path)
- if 'filters' in orig_config:
- display.deprecated(
- 'The `filters` option of the community.hrobot.robot inventory plugin has been renamed to `simple_filters`. '
- 'The old name will stop working in community.hrobot 2.0.0.',
- collection_name='community.hrobot',
- version='2.0.0',
- )
-
self.templar = Templar(loader=loader)
# cache may be True or False at this point to indicate if the inventory is being refreshed
@@ -156,16 +158,26 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
self.populate(servers)
def populate(self, servers):
- filters = self.get_option('simple_filters')
+ simple_filters = self.get_option('simple_filters')
+ filters = parse_filters(self.get_option('filters'))
strict = self.get_option('strict')
server_lists = []
for server in servers:
s = server['server']
server_name = s.get('server_name') or s.get('server_ip') or str(s['server_number'])
- matched = self.filter(s, filters)
+ matched = self.filter(s, simple_filters)
if not matched:
continue
+ facts = {}
+ if 'server_ip' in s:
+ facts['ansible_host'] = make_unsafe(s['server_ip'])
+ for hostvar, hostval in s.items():
+ facts["{0}_{1}".format('hrobot', hostvar)] = make_unsafe(hostval)
+
+ if not filter_host(self, server_name, facts, filters):
+ continue
+
if server_name in server_lists:
display.warning('Two of your Hetzner servers use the same server name ({0}). '
'Please make sure that your server names are unique. '
@@ -174,10 +186,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
self.inventory.add_host(server_name)
server_lists.append(server_name)
- if 'server_ip' in s:
- self.inventory.set_variable(server_name, 'ansible_host', make_unsafe(s['server_ip']))
- for hostvar, hostval in s.items():
- self.inventory.set_variable(server_name, "{0}_{1}".format('hrobot', hostvar), make_unsafe(hostval))
+ for key, value in facts.items():
+ self.inventory.set_variable(server_name, key, value)
# Composed variables
server_vars = self.inventory.get_host(server_name).get_vars()
@@ -189,9 +199,9 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
# Create groups based on variable values and add the corresponding hosts to it
self._add_host_to_keyed_groups(self.get_option('keyed_groups'), server, server_name, strict=strict)
- def filter(self, server, filters):
+ def filter(self, server, simple_filters):
matched = True
- for key, value in filters.items():
+ for key, value in simple_filters.items():
if server.get(key) != value:
matched = False
break