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 "Anything I need to set at ./ansible.cfg?", maybe. In my project folder I've added in ansible.cfg a line with filter_plugins = ./plugins/filter:~/.ansible/plugins/filter. You could in your ansible_dir/ansible.cfg try with filter_plugins = ./filter_plugins:~/.ansible/plugins/filter – U880D Commented Feb 20 at 16:16
  • thank you. Let me generate a default ansible.cfg file, move it to /etc/ansible/ansible.cfg and then add/enable this setting. – JBone Commented Feb 20 at 16:31
  • I have just added ansible.cfg in my ansible_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 have plugins directory in my current ansible_dir (which is my working dir) – JBone Commented Feb 20 at 17:13
Add a comment  | 

1 Answer 1

Reset to default 3

You'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
        }

本文标签: