There seems to be an issue with ansible-inventory
as it reports the ansible_connection
as ssh
for all running instances (and local
for those stopped). You can see it with ansible-inventory -i lxd.yml --list
:
{
"_meta": {
"hostvars": {
"c1": {
"ansible_connection": "ssh",
"ansible_host": "172.24.26.109",
"ansible_lxd_os": "ubuntu",
"ansible_lxd_profile": [
"default"
],
"ansible_lxd_project": "default",
"ansible_lxd_release": "noble",
"ansible_lxd_state": "running",
"ansible_lxd_type": "container"
},
"j1": {
"ansible_connection": "local",
"ansible_lxd_os": "ubuntu",
"ansible_lxd_profile": [
"default"
],
"ansible_lxd_project": "default",
"ansible_lxd_release": "noble",
"ansible_lxd_state": "stopped",
"ansible_lxd_type": "container"
},
}
},
"all": {
"children": [
"ungrouped"
]
},
"ungrouped": {
"hosts": [
"j1",
"c1",
]
}
}
FYI, my lxd.yml
file:
# Usage: ansible-inventory -i lxd.yml --list
plugin: community.general.lxd
url: unix:/var/snap/lxd/common/lxd/unix.socket
That said, if you are OK with a static inventory, it works well:
ansible.cfg
:
$ cat ansible.cfg
[defaults]
inventory = hosts.yml
timeout = 30
forks = 10
# avoids creating multiple sessions with systemd-logind
pipelining = True
[inventory]
enable_plugins = yaml, community.general.lxd
hosts.yml
:
---
all:
vars:
ansible_connection: lxd
ansible_lxd_project: default
ansible_lxd_remote: local
ansible_user: root
ansible_become: false
hosts:
c1:
j1:
books/ping.yml
:
---
- name: ping test
hosts: all
tasks:
- name: test connectivity
ansible.builtin.ping:
$ ansible-playbook books/ping.yml
PLAY [ping test] *******************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************************************************************************************************************************
[WARNING]: Unhandled error in Python interpreter discovery for host j1: instance not running: j1
fatal: [j1]: UNREACHABLE! => {"changed": false, "msg": "instance not running: j1", "unreachable": true}
ok: [c1]
TASK [test connectivity] ***********************************************************************************************************************************************************************************************************************************************************
ok: [c1]
PLAY RECAP *************************************************************************************************************************************************************************************************************************************************************************
c1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
j1 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0