admin管理员组文章数量:1289827
I am trying to develop Custom Filter Plugin. I have a sample one here. I think Ansible is not recognizing the Python class in filter_plugins
directory.
this is the error I am getting now
error "fatal: [localhost]: FAILED! => {"msg": "template error while templating string: Could not load \"a_filter\": 'a_filter'. String: {{ my_text | a_filter }}. Could not load \"a_filter\": 'a_filter'"}
Here is the structure
ansible_dir/
├── filter_plugins
│ └── custom_plugins.py
└── my_playbook.yml
and here is that playbook
---
- hosts: localhost
connection: local
vars:
my_text: "hello world"
tasks:
- name: Print a message
debug:
msg: "{{ my_text | a_filter }}"
and here is the Python class for that filter
#!/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
This is a new installed Ubuntu machine with Python 3. I do have Ansible at /usr/bin/ansible
and Ansible version is
ansible --version
ansible [core 2.16.3]
config file = None
Anything I need to set at /etc/ansible/ansible.cfg
? This file (ansible.cfg
) or ansible
directory does not exist in /etc
directory right now.
I appreciate any inputs.
I am trying to develop Custom Filter Plugin. I have a sample one here. I think Ansible is not recognizing the Python class in filter_plugins
directory.
this is the error I am getting now
error "fatal: [localhost]: FAILED! => {"msg": "template error while templating string: Could not load \"a_filter\": 'a_filter'. String: {{ my_text | a_filter }}. Could not load \"a_filter\": 'a_filter'"}
Here is the structure
ansible_dir/
├── filter_plugins
│ └── custom_plugins.py
└── my_playbook.yml
and here is that playbook
---
- hosts: localhost
connection: local
vars:
my_text: "hello world"
tasks:
- name: Print a message
debug:
msg: "{{ my_text | a_filter }}"
and here is the Python class for that filter
#!/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
This is a new installed Ubuntu machine with Python 3. I do have Ansible at /usr/bin/ansible
and Ansible version is
ansible --version
ansible [core 2.16.3]
config file = None
Anything I need to set at /etc/ansible/ansible.cfg
? This file (ansible.cfg
) or ansible
directory does not exist in /etc
directory right now.
I appreciate any inputs.
Share Improve this question edited Feb 20 at 16:20 U880D 12.1k6 gold badges33 silver badges63 bronze badges asked Feb 20 at 15:59 JBoneJBone 1,8363 gold badges22 silver badges33 bronze badges 3 |1 Answer
Reset to default 3You're simply missing a structural part of the code. Your FilterModule
class has to implement the filters
method which returns a mapping of the filter names defined in your file and the corresponding functions. In you're above case, your custom_plugins.py
file should look like:
class FilterModule(object):
def filters(self):
return {
'a_filter': self.a_filter
}
def a_filter(self, a_variable):
return a_variable + ' custom filter is working'
To go further, if you keep your filtering method as simple as above, it could easily be made static so the following would do the exact same job:
def a_filter(a_variable):
return a_variable + ' custom filter is working'
class FilterModule(object):
def filters(self):
return {
'a_filter': a_filter
}
本文标签:
版权声明:本文标题:ansible - custom filter is not being recognized with the error "template error while templating string: Could not load& 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741423317a2377930.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
./ansible.cfg
?", maybe. In my project folder I've added inansible.cfg
a line withfilter_plugins = ./plugins/filter:~/.ansible/plugins/filter
. You could in youransible_dir/ansible.cfg
try withfilter_plugins = ./filter_plugins:~/.ansible/plugins/filter
– U880D Commented Feb 20 at 16:16ansible.cfg
file, move it to/etc/ansible/ansible.cfg
and then add/enable this setting. – JBone Commented Feb 20 at 16:31ansible.cfg
in myansible_dir
(not in/etc/ansible/ansible.cfg
) with this line[defaults] filter_plugins = ./plugins/filter:~/.ansible/plugins/filter
.. I still get the same error. is this structure specific to your settings?./plugins/filter
the reason I ask is I dont haveplugins
directory in my current ansible_dir (which is my working dir) – JBone Commented Feb 20 at 17:13