Setting connection vars in LXD Inventory

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:

---
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:

    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?)

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:

---
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):

---
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:

{
    "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!

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