Skip to content

Commit da0f90f

Browse files
committed
Add custom Ansible filters
1 parent 7f1e8de commit da0f90f

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
3+
from ansible.errors import AnsibleFilterError
4+
from ansible.module_utils.common.text.converters import to_bool
5+
6+
class FilterModule(object):
7+
def filters(self):
8+
return {
9+
'select_enabled': self.select_enabled
10+
}
11+
12+
# Takes a list of dict
13+
# Returns a list of values of the key 'key' where the key 'enabled' is True
14+
# If key is a list, flatten into single result list
15+
def select_enabled(self, items, key='items'):
16+
result = []
17+
for item in items:
18+
try:
19+
if to_bool(item["enabled"]):
20+
result += item[key] if isinstance(item[key], list) else [item[key]]
21+
except KeyError as e:
22+
raise AnsibleFilterError("Key %s not found in item: %s" % (e, item))
23+
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)