foreman-ansible-modules
foreman-ansible-modules copied to clipboard
Lookup plugin(s)?
SUMMARY
It would be helpful if the resource_info
module (and perhaps host_info
and others) were available as a lookup plugin, to be able to query the foreman api quickly inside a task, rather than having the lookup done in a separate task.
For inspiration, you can take a look at the lookup plugin that's in the netbox collection
https://docs.ansible.com/ansible/latest/collections/netbox/netbox/nb_lookup_lookup.html
ISSUE TYPE
- Feature Idea
Just so that I understand it correctly, you'd want the same data structure as currently returned by the various *_info
modules, but available through a lookup? And then you'd use Jinja and filters to get the data you wanted?
Maybe you could provide some example playbooks how you envision using this?
Yes exactly, the simplest way of implementing this I think would be to keep the same data structure, although it could make sense to return it differently but I'm not sure.
Some usage examples:
- Look up organizations, hostgroups, locations when creating hosts. (This is something that today requires 4 tasks (3 lookups and then the host creation task).)
---
- theforeman.foreman.host:
username: usr
password: psw
server_url: https://my.foreman
name: mynewserver
organization: "{{ query('theforeman.foreman.lookup', 'organizations', api_endpoint='https://my.foreman', username='usr', password='psw' ) | selectattr('name', 'defined') | map(attribute='name') | first }}"
hostgroup: "{{ query('theforeman.foreman.lookup', 'hostgroups', api_endpoint='https://my.foreman', username='usr', password='psw' ) | selectattr('operatingsystem_name', 'equalto', 'RedHat 7.9') | selectattr('subnet_name', 'equalto', 'mysubnet') | map(attribute='name') }}"
locations: "{{ query('theforeman.foreman.lookup', 'locations', api_endpoint='https://my.foreman', username='usr', password='psw' ) | selectattr('description', 'search', 'something funny') | map(attribute='name') }}"
state: present
- Look up subnets when configuring a host interfaces and apply a search parameter to the query. (This is actually the usage case that inspired the idea).
---
- theforeman.foreman.host:
username: usr
password: psw
server_url: https://my.foreman
name: myserver
interfaces_attributes:
- type: "bmc"
name: "ipmi"
subnet: "{{ query('theforeman.foreman.lookup', 'subnets', search='bmc_id=1 boot_mode=DHCP', api_endpoint='https://my.foreman', username='usr', password='psw' ) | map(attribute='network_address') }}"
@mdellweg raised the question whether this should replace the existing *_info
modules, we think probably not, but it's a question that wanted to be asked. (not because you can't run lookups on remote machines).
and the other "obvious" question: how much code can we share between those lookups and the info modules
If it's possible to share most of the code between the two then that would perhaps make it easier to maintain, but could also very well be more work to keep the code compatible with both rather than just maintaining them apart.
Makes me wonder if it would be possible to create a generic ansible plugin that could turn any module into a lookup plugin and vice versa...
Makes me wonder if it would be possible to create a generic ansible plugin that could turn any module into a lookup plugin and vice versa...
Well, lookup plugins are unable to run remote, and if you have a lookup_plugin to wrap modules (to run remote), you'd loose all the speedup benefits for sure.