From da0f90fc997ee96d6773c478b13abda809eed4d7 Mon Sep 17 00:00:00 2001 From: Alex-Welsh Date: Thu, 27 Mar 2025 13:19:35 +0000 Subject: [PATCH] Add custom Ansible filters --- etc/kayobe/ansible.cfg | 2 ++ etc/kayobe/ansible/filter_plugins/filters.py | 23 +++++++++++++++++++ .../group_vars/prometheus-blackbox-exporter | 11 +-------- ...stom-ansible-filters-bad99d417495b7e0.yaml | 8 +++++++ 4 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 etc/kayobe/ansible/filter_plugins/filters.py create mode 100644 releasenotes/notes/custom-ansible-filters-bad99d417495b7e0.yaml diff --git a/etc/kayobe/ansible.cfg b/etc/kayobe/ansible.cfg index e6c3e9c12..c4bf7a0dd 100644 --- a/etc/kayobe/ansible.cfg +++ b/etc/kayobe/ansible.cfg @@ -10,6 +10,8 @@ inject_facts_as_vars = False callbacks_enabled = ansible.posix.profile_tasks # Silence warning about invalid characters found in group names force_valid_group_names = ignore +# Default value plus custom filter plugins path +filter_plugins = $ANSIBLE_HOME/plugins/filter:/usr/share/ansible/plugins/filter:$KAYOBE_CONFIG_PATH/ansible/filter_plugins/ [inventory] # Fail when any inventory source cannot be parsed. diff --git a/etc/kayobe/ansible/filter_plugins/filters.py b/etc/kayobe/ansible/filter_plugins/filters.py new file mode 100644 index 000000000..b672132d6 --- /dev/null +++ b/etc/kayobe/ansible/filter_plugins/filters.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +from ansible.errors import AnsibleFilterError +from ansible.module_utils.common.text.converters import to_bool + +class FilterModule(object): + def filters(self): + return { + 'select_enabled': self.select_enabled + } + + # Takes a list of dict + # Returns a list of values of the key 'key' where the key 'enabled' is True + # If key is a list, flatten into single result list + def select_enabled(self, items, key='items'): + result = [] + for item in items: + try: + if to_bool(item["enabled"]): + result += item[key] if isinstance(item[key], list) else [item[key]] + except KeyError as e: + raise AnsibleFilterError("Key %s not found in item: %s" % (e, item)) + return result diff --git a/etc/kayobe/kolla/inventory/group_vars/prometheus-blackbox-exporter b/etc/kayobe/kolla/inventory/group_vars/prometheus-blackbox-exporter index feea1b993..64d6b867d 100644 --- a/etc/kayobe/kolla/inventory/group_vars/prometheus-blackbox-exporter +++ b/etc/kayobe/kolla/inventory/group_vars/prometheus-blackbox-exporter @@ -6,16 +6,7 @@ # prometheus_blackbox_exporter_endpoints_kayobe is another set of default # endpoints that are templated by Kayobe rather than Kolla Ansible. See # kolla/globals.yml for more details. -prometheus_blackbox_exporter_endpoints_custom: | - {% set endpoints = [] %} - {% for dict_item in (prometheus_blackbox_exporter_endpoints_kayobe | default([]) + stackhpc_prometheus_blackbox_exporter_endpoints_default) %} - {% if dict_item.enabled | bool %} - {% for endpoint in dict_item.endpoints %} - {% set _ = endpoints.append(endpoint) %} - {% endfor %} - {% endif %} - {% endfor %} - {{ (endpoints + stackhpc_prometheus_blackbox_exporter_endpoints_custom) | unique | select | list }} +prometheus_blackbox_exporter_endpoints_custom: "{{ ((prometheus_blackbox_exporter_endpoints_kayobe | default([]) + stackhpc_prometheus_blackbox_exporter_endpoints_default) | select_enabled('endpoints') + stackhpc_prometheus_blackbox_exporter_endpoints_custom) | unique | select | list }}" # A list of custom prometheus Blackbox exporter endpoints. Each element should # have the following format: diff --git a/releasenotes/notes/custom-ansible-filters-bad99d417495b7e0.yaml b/releasenotes/notes/custom-ansible-filters-bad99d417495b7e0.yaml new file mode 100644 index 000000000..8332e01c2 --- /dev/null +++ b/releasenotes/notes/custom-ansible-filters-bad99d417495b7e0.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + Added a new file ``filters.py`` containing custom Ansible filters. + Custom filters can be used to replace complex jinja expressions with + simplified python code. The first filter ``select_enabled`` replaces + an expression to template Blackbox Exporter endpoints. It also serves as an + example for future filters.