Skip to content

Commit b8f6168

Browse files
committed
Add custom Ansible filters
1 parent f7e8c94 commit b8f6168

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

etc/kayobe/ansible.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ inject_facts_as_vars = False
1010
callbacks_enabled = ansible.posix.profile_tasks
1111
# Silence warning about invalid characters found in group names
1212
force_valid_group_names = ignore
13+
# Default value plus custom filter plugins path
14+
filter_plugins = $ANSIBLE_HOME/plugins/filter:/usr/share/ansible/plugins/filter:$KAYOBE_CONFIG_PATH/ansible/filter_plugins/
1315

1416
[inventory]
1517
# Fail when any inventory source cannot be parsed.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
3+
from ansible.errors import AnsibleFilterError
4+
from ansible.plugins.filter.core import to_bool
5+
6+
class FilterModule(object):
7+
def filters(self):
8+
return {
9+
'select_enabled': self.select_enabled
10+
}
11+
12+
def select_enabled(self, items: list[dict], key: str = 'items') -> list:
13+
"""Filters a list of dictionaries to select enabled items.
14+
15+
Parameters:
16+
items:
17+
List of dictionaries - Each item contains an 'enabled' key, and
18+
a key matching the "key" parameter.
19+
key:
20+
String - The key in each dictionary to extract when 'enabled'
21+
is true.
22+
23+
Returns:
24+
A list of values from the specified key in each dictionary where
25+
'enabled' is true. List values are flattened.
26+
"""
27+
result = []
28+
for item in items:
29+
try:
30+
if to_bool(item["enabled"]):
31+
result += item[key] if isinstance(item[key], list) else [item[key]]
32+
except KeyError as e:
33+
raise AnsibleFilterError("Key %s not found in item: %s" % (e, item))
34+
return result

etc/kayobe/kolla/inventory/group_vars/prometheus-blackbox-exporter

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@
66
# prometheus_blackbox_exporter_endpoints_kayobe is another set of default
77
# endpoints that are templated by Kayobe rather than Kolla Ansible. See
88
# kolla/globals.yml for more details.
9-
prometheus_blackbox_exporter_endpoints_custom: |
10-
{% set endpoints = [] %}
11-
{% for dict_item in (prometheus_blackbox_exporter_endpoints_kayobe | default([]) + stackhpc_prometheus_blackbox_exporter_endpoints_default) %}
12-
{% if dict_item.enabled | bool %}
13-
{% for endpoint in dict_item.endpoints %}
14-
{% set _ = endpoints.append(endpoint) %}
15-
{% endfor %}
16-
{% endif %}
17-
{% endfor %}
18-
{{ (endpoints + stackhpc_prometheus_blackbox_exporter_endpoints_custom) | unique | select | list }}
9+
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 }}"
1910

2011
# A list of custom prometheus Blackbox exporter endpoints. Each element should
2112
# have the following format:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
features:
3+
- |
4+
Added a new file ``filters.py`` containing custom Ansible filters.
5+
Custom filters can be used to replace complex jinja expressions with
6+
simplified python code. The first filter ``select_enabled`` replaces
7+
an expression to template Blackbox Exporter endpoints. It also serves as an
8+
example for future filters.

0 commit comments

Comments
 (0)