Filter plugin can no longer be found when using ansible collections

112 views Asked by At

I have a repository I am using to host an ansible collection, it contains an example task within the t1 role, using a custom filter plugin I have created:

---
# tasks file for t1
- name: "something"
  debug:
    msg: "{{ 'this is a message' | something }}"

Within the collection as follows

.
├── docs
├── galaxy.yml
├── meta
│   └── runtime.yml
├── plugins
│   └── README.md
├── README.md
├── roles
│   └── t1
│       ├── defaults
│       │   └── main.yml
│       ├── files
│       ├── filter_plugins
│       │   └── filter.py
│       ├── handlers
│       │   └── main.yml
│       ├── meta
│       │   └── main.yml
│       ├── README.md
│       ├── tasks
│       │   └── main.yml
│       ├── templates
│       ├── tests
│       │   ├── inventory
│       │   └── test.yml
│       └── vars
│           └── main.yml
└── run_role_locally.yaml

When I run the run_role_locally.yaml playbook - all works as expected. I am very happy that my filter_plugin is "packaged" within the role.

class FilterModule(object):

    def filters(self):
        return {
            'something': self.something_method
        }
    
    def something_method(self, message):
        return f"{message}, never gonna give you up"

Thus


PLAY [localhost] *****************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************
ok: [localhost]

TASK [t1 : something] ************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "this is a message, never gonna give you up"
}

PLAY RECAP ***********************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

However to keep these things decoupled, I have another repository I am using to use the collection as follows:

.
├── playbook.yaml
├── README.md
└── requirements.yaml

I install the collection successfully, and locally in an adjacent folder

ansible-galaxy collection install -r requirements.yaml -p ./collections
---
collections:
  - name: https://github.com/my_stuff/platform_collections.git
    type: git
    version: release/0.0.0

When I run my playbook:

---
- name: demo
  hosts: localhost
  connection: local
  become: false

  roles:
  - role: platform.test.t1

Ansible can no longer find the filter plugin:

PLAY [demo] **********************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************
ok: [localhost]

TASK [platform.test.t1 : something] **********************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "template error while templating string: Could not load \"something\": 'something'. String: {{ 'this is a message' | something }}. Could not load \"something\": 'something'"}

PLAY RECAP ***********************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

Must I relocate the filter plugin to a filter directory or can I keep it within the role? I have attempted both unsuccessfully.

0

There are 0 answers