Hey everyone,
I would like to share with the community a very flexible Ansible dynamic inventory script https://github.com/vosdev/ansible-lxd-inventory
The script allows creating an inventory from multiple LXD nodes/clusters with fine-grained filtering to suit your needs.
I’m using it to create a single inventory from 4 standalone LXD hosts and a multi-node cluster. The ansible lxd inventory shipped with ansible required over 20 inventory files plus several workarounds to almost achieve the same! My ansible management environment (SemaphoreUI) is finally cleaned up.
A feature I like very much is the ability to template the instance name returned to Ansible. ({name}.{project}.{endpoint}.example.com) So instead of monitoring it will return monitoring.production.cluster-a.example.com.
You can ignore interfaces to make sure the ip address of for example docker0 is not returned as your instance’s ip address. By default the first IPv4 is returned but this can be configured to prefer IPv6.
You probably do not want ALL your LXD guests to be in your inventory, so you can filter these out through the use of:
- Status [Running, Stopped, Error, Frozen]
- include/exclude projects
- exclude based on name
- include/exclude based on
user.*keys - include/exclude based on profiles
- vm and/or lxc
Personally I only include running instances. I also have some instances that I don’t want managed (short tests, appliances or Windows guests) that I have given a user.ansible=false key to keep them out.
The script is already very feature-rich but I’m curious to hear if there are any features I missed that you would like to use.