# Setting connection vars in LXD Inventory

**URL:** <https://forum.ansible.com/t/setting-connection-vars-in-lxd-inventory/44737>\
**Category:** Get Help\
**Tags:** windows, ubuntu, inventory\
**Created:** [October 24, 2025, 2:50pm UTC](https://forum.ansible.com/t/setting-connection-vars-in-lxd-inventory/44737 "2025-10-24T14:50:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkettman](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dkettman/32/9709_2.png) [@dkettman](https://forum.ansible.com/u/dkettman)\
**Post date:** [October 24, 2025, 2:50pm UTC](https://forum.ansible.com/t/setting-connection-vars-in-lxd-inventory/44737/1 "2025-10-24T14:50:55Z")

</div>

Hello,

I am working on some lab building Ansible playbooks and using Incus/LXD for the “hypervisor”. Some of my VMs are Windows machines which I want to use the `psrp` module to connect to them. This requires setting some variables for these hosts. What I have attempted is this for my inventory:

```auto
---
plugin: community.general.lxd
url: unix:/var/lib/incus/unix.socket
type_filter: both
state: RUNNING
groupby:
  windows:
    type: profile
    attribute: windows
    ansible_connection: psrp
    ansible_user: ansible
    ansible_password: --ReDaCtEd--
  ubuntu:
    type: os
    attribute: ubuntu

```

As you can see, I’m specifying the Incus socket to use rather than the LXD as there is currently no Incus-specific Inventory plugin at this time. Essentially, if they have the ‘windows’ profile added to their container/vm, then they need to have the following defined:

```auto
    ansible_connection: psrp
    ansible_user: ansible
    ansible_password: --ReDaCtEd--

```

Is there another way to make sure the systems in the ‘windows’ group have these defined prior to connection time?

- tags
  - Inventory
  - LXC/LXD
  - Incus
  - Hostvars (? maybe?)

---

<div class="post-metadata">

**Author:** ![dkettman](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dkettman/32/9709_2.png) [@dkettman](https://forum.ansible.com/u/dkettman)\
**Post date:** [October 24, 2025, 3:04pm UTC](https://forum.ansible.com/t/setting-connection-vars-in-lxd-inventory/44737/2 "2025-10-24T15:04:01Z")

</div>

Still digging into this, it looks like in the `lxd.py` inventory script in the community.general collection is part of the cause. There is a line (about line 676) where `ansible_connection` is set to `ssh` so long as there is a network interface. This does not allow that value to be reassigned prior to running my playbook. If I comment out the offending line in the `lxd.py` that hard-codes the `ansible_connection` to be `ssh` and then set a group var in another static inventory file, it will reassign it.

So at the moment, I have commented out the line hard-coding the value of `ansible_connection`. I have my `lxd.yaml` file:

```auto
---
plugin: community.general.lxd
url: unix:/var/lib/incus/unix.socket
type_filter: both
state: RUNNING
groupby:
  windows:
    type: profile
    attribute: windows
  ubuntu:
    type: os
    attribute: ubuntu

```

and my ‘static’ inventory (`inventory.yaml`):

```auto
---
windows:
  vars:
    ansible_connection: psrp
    ansible_user: ansible
    ansible_password: --ReDaCtEd--

```

When I run `ansible-inventory -i lxd.yaml -i inventory.yaml --host win2k22`, I get:

```auto
{
    "ansible_connection": "psrp",
    "ansible_host": "10.202.124.212",
    "ansible_lxd_profile": [
        "default",
        "windows"
    ],
    "ansible_lxd_project": "default",
    "ansible_lxd_state": "running",
    "ansible_lxd_type": "virtual-machine",
    "ansible_password": "--ReDaCtEd--",
    "ansible_user": "ansible"
}

```

I think what would make the most sense here is to have a defined default if it isn’t overridden. Also, possibly being able to define hostvars in the lxd.yaml file would be nice so I don’t need to make sure the second inventory file is there.

I would appreciate someone confirming that what I’m doing is “correct”. Also, if someone could confirm whether or not in other dynamic inventories if we can define additional variables such as how I initially tried in my first post.

Thanks!

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [October 27, 2025, 5:07pm UTC](https://forum.ansible.com/t/setting-connection-vars-in-lxd-inventory/44737/3 "2025-10-27T17:07:28Z")

</div>

> <https://github.com/ansible-collections/community.general/issues/10967>
>
> \### Summary
> 
> While using the \`community.general.lxd\` inventory plugin, I am unab…le to assign any variables group. For instance, here is my lxd.yaml file:
> \`\`\`yaml
> \---
> plugin: community.general.lxd
> url: unix:/var/lib/incus/unix.socket
> type\_filter: both
> state: RUNNING
> groupby:
> windows:
> type: profile
> attribute: windows
> ansible\_user: ansible
> ansible\_password: --ReDaCtEd--
> ubuntu:
> type: os
> attribute: ubuntu
> \`\`\`
> I would expect when running \`ansible-inventory -i lxd.yaml --list\` I would see my \`ansible\_user\` and \`ansible\_password\` variables defined. Instead they seem to be ignored. Output:
> \`\`\`
> (ansible) dkettman@desktop:~/ansible$ ansible-inventory -i lxd.yaml --list
> {
> "\_meta": {
> "hostvars": {
> "amslab-dc01": {
> "ansible\_host": {
> "\_\_ansible\_unsafe": "10.202.124.221"
> },
> "ansible\_lxd\_os": {
> "\_\_ansible\_unsafe": "windows"
> },
> "ansible\_lxd\_profile": \[
> {
> "\_\_ansible\_unsafe": "default"
> },
> {
> "\_\_ansible\_unsafe": "windows"
> }
> \],
> "ansible\_lxd\_project": {
> "\_\_ansible\_unsafe": "default"
> },
> "ansible\_lxd\_release": {
> "\_\_ansible\_unsafe": "server 2022"
> },
> "ansible\_lxd\_state": {
> "\_\_ansible\_unsafe": "running"
> },
> "ansible\_lxd\_type": {
> "\_\_ansible\_unsafe": "virtual-machine"
> }
> }
> },
> "profile": "inventory\_legacy"
> },
> "all": {
> "children": \[
> "ungrouped",
> "windows",
> "ubuntu"
> \]
> },
> "windows": {
> "hosts": \[
> "amslab-dc01"
> \]
> }
> }
> (ansible) dkettman@desktop:~/ansible$
> \`\`\`
> I saw in a thread elsewhere for another dynamic inventory to define the groupvars in a static inventory file. When attempting that, I found that it worked for all variables except for the \`ansible\_connection\` variable. I was able to define my other variables, but this one stayed the same. 
> 
> inventory.yaml:
> \`\`\`yaml
> \---
> windows:
> vars:
> ansible\_connection: psrp
> ansible\_user: ansible
> ansible\_password: --ReDaCtEd--
> ansible\_psrp\_cert\_validation: ignore
> ansible\_psrp\_protocol: http
> \`\`\`
> 
> So when I run \`ansible-inventory -i lxd.yaml -i inventory.yaml --list\` I now see:
> \`\`\`json
> {
> "\_meta": {
> "hostvars": {
> "amslab-dc01": {
> "ansible\_connection": "ssh",
> "ansible\_host": {
> "\_\_ansible\_unsafe": "10.202.124.221"
> },
> "ansible\_lxd\_os": {
> "\_\_ansible\_unsafe": "windows"
> },
> "ansible\_lxd\_profile": \[
> {
> "\_\_ansible\_unsafe": "default"
> },
> {
> "\_\_ansible\_unsafe": "windows"
> }
> \],
> "ansible\_lxd\_project": {
> "\_\_ansible\_unsafe": "default"
> },
> "ansible\_lxd\_release": {
> "\_\_ansible\_unsafe": "server 2022"
> },
> "ansible\_lxd\_state": {
> "\_\_ansible\_unsafe": "running"
> },
> "ansible\_lxd\_type": {
> "\_\_ansible\_unsafe": "virtual-machine"
> },
> "ansible\_password": "--ReDaCtEd--",
> "ansible\_psrp\_cert\_validation": "ignore",
> "ansible\_psrp\_protocol": "http",
> "ansible\_user": "ansible"
> }
> },
> "profile": "inventory\_legacy"
> },
> "all": {
> "children": \[
> "ungrouped",
> "windows",
> "ubuntu"
> \]
> },
> "windows": {
> "hosts": \[
> "amslab-dc01"
> \]
> }
> }
> \`\`\`
> 
> Note that in the inventory.yaml file, I am specifying that \`ansible\_connection: psrp\` but in my resolved inventory, it is \`ansible\_connection: ssh\`
> 
> I found in the \`lxd.py\` file this: 
> 
> https://github.com/ansible-collections/community.general/blob/a3987c9844c1afaba4d3234b52b114db0c641240/plugins/inventory/lxd.py#L674C9-L678C86
> 
> If I comment out the line setting the ansible\_connection statically to 'ssh', I am able to re-assign it. Why would we be statically overriding the connection in this way?
> 
> Wasn't sure whether to do a bug or a feature. On one hand, you can't define anything based on the host groups in the dynamic inventory file, so you use a static inventory file. This seems like it could be resolved with a feature request (possibly?). On the other hand is an actual bug: The inventory isn't allowing me to define how to connect to a host just because it is an LXD (or Incus in my case) container/VM.
> 
> \### Issue Type
> 
> Bug Report
> 
> \### Component Name
> 
> lxd
> 
> \### Ansible Version
> 
> \`\`\`console (paste below)
> (ansible) dkettman@desktop:~/ansible$ ansible --version
> ansible \[core 2.19.3\]
> config file = /home/dkettman/.ansible.cfg
> configured module search path = \['/home/dkettman/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'\]
> ansible python module location = /home/dkettman/ansible/lib/python3.12/site-packages/ansible
> ansible collection location = /home/dkettman/.ansible/collections:/usr/share/ansible/collections
> executable location = /home/dkettman/ansible/bin/ansible
> python version = 3.12.3 (main, Aug 14 2025, 17:47:21) \[GCC 13.3.0\] (/home/dkettman/ansible/bin/python3)
> jinja version = 3.1.6
> pyyaml version = 6.0.3 (with libyaml v0.2.5)
> \`\`\`
> 
> 
> \### Community.general Version
> 
> \`\`\`console (paste below)
> $ ansible-galaxy collection list community.general
> 
> \# /home/dkettman/.ansible/collections/ansible\_collections
> Collection Version
> \----------------- -------
> community.general 11.4.0 
> \`\`\`
> 
> 
> \### Configuration
> 
> \`\`\`console (paste below)
> (ansible) dkettman@desktop:~/ansible$ ansible-config dump --only-changed
> CONFIG\_FILE() = /home/dkettman/.ansible.cfg
> EDITOR(env: EDITOR) = /usr/bin/vim
> HOST\_KEY\_CHECKING(/home/dkettman/.ansible.cfg) = False
> 
> GALAXY\_SERVERS:
> \`\`\`
> 
> 
> \### OS / Environment
> 
> Control Node: Ubuntu 24.04 LTS
> Client Node: Windows Server 2022 (psrp connection)
> 
> \### Steps to Reproduce
> 
> While everything is in the description, the "bug" portion of this is that I am unable to "override" the connection type when using the lxd inventory as written.
> lxd.yaml:
> \`\`\`yaml
> \---
> plugin: community.general.lxd
> url: unix:/var/lib/incus/unix.socket
> type\_filter: both
> state: RUNNING
> groupby:
> windows:
> type: profile
> attribute: windows
> ansible\_user: ansible
> ansible\_password: --ReDaCtEd--
> ubuntu:
> type: os
> attribute: ubuntu
> \`\`\`
> inventory.yaml:
> \`\`\`yaml
> \---
> windows:
> vars:
> ansible\_connection: psrp
> ansible\_user: ansible
> ansible\_password: --ReDaCtEd--
> ansible\_psrp\_cert\_validation: ignore
> ansible\_psrp\_protocol: http
> \`\`\`
> 
> 
> \### Expected Results
> 
> I expected my \`ansible\_connection\` value for hosts in the 'windows' host group to have the \`ansible\_connection\` value set per my inventory.yaml file.
> 
> \### Actual Results
> 
> \`\`\`console (paste below)
> (ansible) dkettman@desktop:~/ansible$ ansible-inventory -i lxd.yaml -i inventory.yaml --list
> {
> "\_meta": {
> "hostvars": {
> "amslab-dc01": {
> "ansible\_connection": {
> "\_\_ansible\_unsafe": "ssh"
> },
> "ansible\_host": {
> "\_\_ansible\_unsafe": "10.202.124.221"
> },
> "ansible\_lxd\_os": {
> "\_\_ansible\_unsafe": "windows"
> },
> "ansible\_lxd\_profile": \[
> {
> "\_\_ansible\_unsafe": "default"
> },
> {
> "\_\_ansible\_unsafe": "windows"
> }
> \],
> "ansible\_lxd\_project": {
> "\_\_ansible\_unsafe": "default"
> },
> "ansible\_lxd\_release": {
> "\_\_ansible\_unsafe": "server 2022"
> },
> "ansible\_lxd\_state": {
> "\_\_ansible\_unsafe": "running"
> },
> "ansible\_lxd\_type": {
> "\_\_ansible\_unsafe": "virtual-machine"
> },
> "ansible\_password": "New123Pass!!",
> "ansible\_psrp\_cert\_validation": "ignore",
> "ansible\_psrp\_protocol": "http",
> "ansible\_user": "ansible"
> }
> },
> "profile": "inventory\_legacy"
> },
> "all": {
> "children": \[
> "ungrouped",
> "windows",
> "ubuntu"
> \]
> },
> "windows": {
> "hosts": \[
> "amslab-dc01"
> \]
> }
> }
> \`\`\`
> 
> 
> \### Code of Conduct
> 
> \- \[x\] I agree to follow the Ansible Code of Conduct

TLDR: Right now it’s not possible, the plugin doesn’t implement the Constructed interface.
