# Accessing inventory variables from custom connection plugin

**URL:** <https://forum.ansible.com/t/accessing-inventory-variables-from-custom-connection-plugin/38340>\
**Category:** Ansible Developer\
**Created:** [August 16, 2023, 10:00am UTC](https://forum.ansible.com/t/accessing-inventory-variables-from-custom-connection-plugin/38340 "2023-08-16T10:00:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Piotr\_Gabrys](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@Piotr\_Gabrys](https://forum.ansible.com/u/Piotr_Gabrys)\
**Post date:** [August 16, 2023, 10:00am UTC](https://forum.ansible.com/t/accessing-inventory-variables-from-custom-connection-plugin/38340/1 "2023-08-16T10:00:21Z")

</div>

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](https://gist.github.com/Soothly/183f419d7a26b11e58292950134a23ef)

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](https://github.com/xlab-steampunk/ansible-pms-plugins/tree/master/connection) 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ś

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 16, 2023, 6:10pm UTC](https://forum.ansible.com/t/accessing-inventory-variables-from-custom-connection-plugin/38340/2 "2023-08-16T18:10:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 16, 2023, 6:12pm UTC](https://forum.ansible.com/t/accessing-inventory-variables-from-custom-connection-plugin/38340/3 "2023-08-16T18:12:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Piotr\_Gabrys](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@Piotr\_Gabrys](https://forum.ansible.com/u/Piotr_Gabrys)\
**Post date:** [August 17, 2023, 5:46am UTC](https://forum.ansible.com/t/accessing-inventory-variables-from-custom-connection-plugin/38340/4 "2023-08-17T05:46:00Z")

</div>

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