You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible自定义过滤器无法识别,报template error加载失败求助

Ansible自定义过滤器无法识别的解决办法

核心问题:FilterModule类缺少必填的filters()方法

Ansible自定义过滤器的核心要求是:FilterModule类必须实现filters()方法,该方法返回一个字典,将过滤器名称映射到对应的处理方法。你的代码仅定义了a_filter方法,但未通过filters()方法暴露给Ansible,导致无法被加载。

修改后的过滤器代码

#!/usr/bin/python3
class FilterModule(object):

    def a_filter(self, a_variable):
        a_new_variable = a_variable + ' custom filter is working'
        return a_new_variable

    def filters(self):
        # 将过滤器名称与对应方法绑定
        return {
            'a_filter': self.a_filter
        }

关于ansible.cfg的说明

默认情况下,Ansible会自动识别playbook同级目录下的filter_plugins目录(你的目录结构完全符合规则),因此不需要额外创建/etc/ansible/ansible.cfg文件。只有当你需要全局自定义Ansible配置时,才需要创建该文件。

额外验证步骤

  • 确认filter_plugins目录名称完全正确(大小写敏感,必须严格匹配)
  • 重新执行playbook:ansible-playbook my_playbook.yml

内容的提问来源于stack exchange,提问作者JBone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 08:27:08