Accessing inventory variables from custom connection plugin

Hi,
I am developing a new Ansible collection to manage configuration of network devices. These devices are configured only over API. I need to be able to define them in an inventory file. From my understanding, I should create a new connection plugin and use it to connect to devices.

The problem starts when I try to access values for my connection plugin configuration options. These values are device-specific so it would be best to define them in an inventory file.

When I run a playbook that uses config_module I am getting an error:

fatal: [device_1]: FAILED! => {“msg”: "No setting was provided for required configuration plugin_type: connection plugin: plugin_name setting: address "}

My code example

I am running Ansible 8.2 [ansible-core 2.15.2]

I’ve noticed that my other calls to self.get_option(option) always return default value from connection plugin documentation irrespective of what is defined in the inventory file. I’ve checked that these inventory variables are available at playbook level. I am able to use debug module to print their values.

How do I access inventory variables for a given host in my connection plugin?

It seems to have been possible in Ansible 2.9
I could pass them from the playbook to config_module as arguments and define ansible_connection: local for each device in inventory but it:

  • raises separation of concerns issue (module for configuration should not care about connection details)
  • adds additional arguments for each task with configuration module (there will be many in a playbook)
  • means that the connection will not be persistent between tasks

Any ideas?

Best Regards
Piotr Gabryś

I'm not sure what you are trying to do, but it looks like you are
trying to instantiate a connection plugin from a module, this is not
normally how things work.

Connection plugins are provided the vars they declare in config when
they are loaded from the task executor, trying to load/create it from
the module you bypass all this.
Also modules do not have any data not explicitly passed to them, as
they are designed to run on the 'target' and you don't want to
disclose on 'hostA' all the secrets for hostsB to hostsX, so of course
they do not have access to the variables you need.

If this was possible in 2.9, which it was not, it would have been a
major bug and security issue.

The 'working' example you show seems to get a full dictionary of the
parameters passed to the module via the 'endpoint' option, I don't see
any other way that would work.

forgot to add, you might want to look at httpapi (netcli, netconf)
plugins, which are a variant on normal connection plugins that uses a
'local execution' and then passes the connection to the module.

Another option is 'turbo' mode from module utils which can cache
connections and cloud libraries.

Thank you Brian!
I thought that I had to be missing some important concept. I need to investigate httpapi plugins then