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:

β”œβ”€β”€ README.md
β”œβ”€β”€ examples
β”‚   └── test.yml
└── plugins
    └── lookup
        └── my_plugin.py
└── 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
  collections:
    - my_namespace.my_collection_name

  vars:
    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:

[defaults]
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?

2 Likes

@trevor

Hello.

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:

https://docs.ansible.com/ansible/latest/collections_guide/collections_using_playbooks.html#using-collections-in-playbooks

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

3 Likes

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

1 Like

Yep. That did it. Thank you!

2 Likes

My pleasure :grinning:

I’m glad it went well.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.