Skip to content

Commit 2b46fa4

Browse files
committed
Add custom Ansible filters
1 parent 9366718 commit 2b46fa4

File tree

3 files changed

+31
-10
lines changed

3 files changed

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