Help with using a lookup plugin from a collection

trevor
Fri, 26 Jan 2024 19:03:40 +0000
Get Help

Hello

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?

6 posts - 3 participants