Help with using a lookup plugin from a collection

Hello :wave:

Apologies in advance if this is a super basic question, but I’ve been struggling with getting this to work the way I want it to for a while.

I am developing an Ansible lookup plugin for internal use at my company. We have a Galaxy API server that I am supposed to upload it to. My collection that contains the lookup plugin looks like the following:

├── examples
│   └── test.yml
└── plugins
    └── lookup
└── galaxy.yml

I can build and install the collection just fine. It installs to $HOME/.ansible/collections/my_namespace/my_collection_name.

Here is a sample of how I’m trying to use the lookup plugin from a playbook:

- name: A test playbook
  hosts: localhost
  gather_facts: false
    - my_namespace.my_collection_name

    some_var_from_my_plugin: "{{ lookup('my_plugin', 'the_var_im_looking_for') }}"

# ...

However, I always get this error:

fatal: [localhost]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ lookup('my_plugin', 'the_var_im_looking_for') }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: lookup plugin (my_plugin) not found"}

I can work around this if I set the following in my ansible.cfg file, but this isn’t a practical solution for how other users in the org will be using it:

lookup_plugins = /path/to/my/home/.ansible/collections/my_namespace/my_collection_name/plugins/lookup

What is the correct way to invoke a lookup plugin from a collection?




In this case, the collections keyword may not work.

How about using FQCN as follow?

    some_var_from_my_plugin: "{{ lookup('my_namespace.my_collection_name.my_plugin', 'the_var_im_looking_for') }}"

See also:

Note that an FQCN is still required for non-action or module plugins (for example, lookups, filters, and tests).


Thank you! I’ll give this a try tomorrow.

1 Like

Yep. That did it. Thank you!


My pleasure :grinning:

I’m glad it went well.