Skip to content

Add first custom Ansible filter #1800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: stackhpc/2025.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions etc/kayobe/ansible.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions etc/kayobe/ansible/filter_plugins/filters.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading