| 1 |
# Copyright (c) 2025 Stéphane Graber <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
name: incus |
| 9 |
short_description: Incus inventory source |
| 10 |
version_added: 12.0.0 |
| 11 |
author: |
| 12 |
- Stéphane Graber (@stgraber) |
| 13 |
requirements: |
| 14 |
- Incus CLI (C(incus)) |
| 15 |
description: |
| 16 |
- Get inventory hosts from the Incus container and virtual-machine manager. |
| 17 |
options: |
| 18 |
plugin: |
| 19 |
description: |
| 20 |
- The name of this plugin, it should always be set to community.general.incus for this plugin to work. |
| 21 |
required: true |
| 22 |
choices: ['community.general.incus'] |
| 23 |
type: str |
| 24 |
default_groups: |
| 25 |
description: |
| 26 |
- Whether to generate default groups based on remote and project. |
| 27 |
type: bool |
| 28 |
default: true |
| 29 |
filters: |
| 30 |
description: |
| 31 |
- Filter expression as supported by C(incus list). |
| 32 |
type: list |
| 33 |
elements: string |
| 34 |
default: [] |
| 35 |
host_domain: |
| 36 |
description: |
| 37 |
- Domain to append to the host. |
| 38 |
- This is also used when O(host_fqdn) is not set since community.general 12.5.0. |
| 39 |
type: string |
| 40 |
host_fqdn: |
| 41 |
description: |
| 42 |
- Whether to generate a FQDN for the host name. |
| 43 |
- This will use the INSTANCE.PROJECT.REMOTE syntax. |
| 44 |
type: bool |
| 45 |
default: true |
| 46 |
remotes: |
| 47 |
description: |
| 48 |
- The names of the Incus remotes to use (per C(incus remote list)). |
| 49 |
- Remotes are used to access multiple servers from a single client. |
| 50 |
- By default the inventory will go over all projects for each remote. |
| 51 |
- It is possible to specify a specific project using V(remote:project). |
| 52 |
type: list |
| 53 |
elements: string |
| 54 |
default: ["local"] |
| 55 |
extends_documentation_fragment: |
| 56 |
- ansible.builtin.constructed |
| 57 |
""" |
| 58 |
|
| 59 |
EXAMPLES = r""" |
| 60 |
--- |
| 61 |
# Pull instances from all projects on the local remote. |
| 62 |
plugin: community.general.incus |
| 63 |
|
| 64 |
--- |
| 65 |
# Pull running VMs from all projects on the local remote. |
| 66 |
plugin: community.general.incus |
| 67 |
filters: |
| 68 |
- type=virtual-machine |
| 69 |
- status=running |
| 70 |
|
| 71 |
--- |
| 72 |
# Pull instances from two different remotes |
| 73 |
plugin: community.general.incus |
| 74 |
remotes: |
| 75 |
- remote-1 |
| 76 |
- remote-2 |
| 77 |
|
| 78 |
--- |
| 79 |
# Pull instances from two different remotes |
| 80 |
# Limiting the second to the default project |
| 81 |
plugin: community.general.incus |
| 82 |
remotes: |
| 83 |
- remote-1 |
| 84 |
- remote-2:default |
| 85 |
""" |
| 86 |
|
| 87 |
from json import loads |
| 88 |
from subprocess import check_output |
| 89 |
|
| 90 |
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable |
| 91 |
from ansible.utils.display import Display |
| 92 |
|
| 93 |
display = Display() |
| 94 |
|
| 95 |
|
| 96 |
class InventoryModule(BaseInventoryPlugin, Constructable): |
| 97 |
"""Host inventory parser for Incus.""" |
| 98 |
|
| 99 |
NAME = "community.general.incus" |
| 100 |
|
| 101 |
def __init__(self): |
| 102 |
super().__init__() |
| 103 |
|
| 104 |
def verify_file(self, path): |
| 105 |
valid = False |
| 106 |
|
| 107 |
if super().verify_file(path): |
| 108 |
if path.endswith(("incus.yaml", "incus.yml")): |
| 109 |
valid = True |
| 110 |
else: |
| 111 |
self.display.vvv('Skipping due to inventory source not ending in "incus.yaml" nor "incus.yml"') |
| 112 |
|
| 113 |
return valid |
| 114 |
|
| 115 |
def parse(self, inventory, loader, path, cache=True): |
| 116 |
super().parse(inventory, loader, path) |
| 117 |
|
| 118 |
self._read_config_data(path) |
| 119 |
|
| 120 |
self.populate() |
| 121 |
|
| 122 |
def populate(self): |
| 123 |
# Create top-level "incus" group if missing. |
| 124 |
default_groups = self.get_option("default_groups") |
| 125 |
if default_groups: |
| 126 |
self.inventory.add_group("incus") |
| 127 |
|
| 128 |
for remote in self.get_option("remotes"): |
| 129 |
# Split the remote name from the project name (if specified). |
| 130 |
remote_name = "" |
| 131 |
project_name = "" |
| 132 |
|
| 133 |
fields = remote.split(":", 1) |
| 134 |
if len(fields) == 2: |
| 135 |
remote_name = fields[0] |
| 136 |
project_name = fields[1] |
| 137 |
else: |
| 138 |
remote_name = fields[0] |
| 139 |
|
| 140 |
# Create the remote-specific group if missing. |
| 141 |
group_remote = f"incus_{remote_name}" |
| 142 |
if default_groups: |
| 143 |
self.inventory.add_group(group_remote) |
| 144 |
self.inventory.add_child("incus", group_remote) |
| 145 |
|
| 146 |
# Get a list of projects. |
| 147 |
projects = [] |
| 148 |
if project_name: |
| 149 |
projects = [project_name] |
| 150 |
else: |
| 151 |
projects = [entry["name"] for entry in self._run_incus("project", "list", f"{remote_name}:")] |
| 152 |
|
| 153 |
# Get a list of instances. |
| 154 |
for project in projects: |
| 155 |
# Create the project-specific group if missing. |
| 156 |
group_project = f"{group_remote}_{project}" |
| 157 |
if default_groups: |
| 158 |
self.inventory.add_group(group_project) |
| 159 |
self.inventory.add_child(group_remote, group_project) |
| 160 |
|
| 161 |
# List the instances. |
| 162 |
list_cmd = [ |
| 163 |
"list", |
| 164 |
f"{remote_name}:", |
| 165 |
"--project", |
| 166 |
project, |
| 167 |
] + self.get_option("filters") |
| 168 |
for instance in self._run_incus(*list_cmd): |
| 169 |
# Compute the host name. |
| 170 |
host_name = instance["name"] |
| 171 |
if self.get_option("host_fqdn"): |
| 172 |
host_name = f"{host_name}.{project}.{remote_name}" |
| 173 |
|
| 174 |
domain = self.get_option("host_domain") |
| 175 |
if domain: |
| 176 |
host_name = f"{host_name}.{domain}" |
| 177 |
|
| 178 |
# Add some extra variables. |
| 179 |
host_vars = {} |
| 180 |
host_vars["ansible_incus_remote"] = remote_name |
| 181 |
host_vars["ansible_incus_project"] = project |
| 182 |
|
| 183 |
for prop in ( |
| 184 |
"architecture", |
| 185 |
"config", |
| 186 |
"description", |
| 187 |
"devices", |
| 188 |
"ephemeral", |
| 189 |
"expanded_config", |
| 190 |
"expanded_devices", |
| 191 |
"location", |
| 192 |
"profiles", |
| 193 |
"status", |
| 194 |
"type", |
| 195 |
): |
| 196 |
host_vars[f"ansible_incus_{prop}"] = instance[prop] |
| 197 |
|
| 198 |
# Add the host to the inventory and constructed groups. |
| 199 |
self._add_host(host_name, host_vars) |
| 200 |
|
| 201 |
# Add the host to the built-in groups. |
| 202 |
if default_groups: |
| 203 |
self.inventory.add_host(host_name, group_project) |
| 204 |
|
| 205 |
def _add_host(self, hostname, host_vars): |
| 206 |
self.inventory.add_host(hostname, group="all") |
| 207 |
|
| 208 |
for var_name, var_value in host_vars.items(): |
| 209 |
self.inventory.set_variable(hostname, var_name, var_value) |
| 210 |
|
| 211 |
strict = self.get_option("strict") |
| 212 |
|
| 213 |
# Add variables created by the user's Jinja2 expressions to the host |
| 214 |
self._set_composite_vars(self.get_option("compose"), host_vars, hostname, strict=True) |
| 215 |
|
| 216 |
# Create user-defined groups using variables and Jinja2 conditionals |
| 217 |
self._add_host_to_composed_groups(self.get_option("groups"), host_vars, hostname, strict=strict) |
| 218 |
self._add_host_to_keyed_groups(self.get_option("keyed_groups"), host_vars, hostname, strict=strict) |
| 219 |
|
| 220 |
def _run_incus(self, *args): |
| 221 |
local_cmd = ["incus"] + list(args) + ["--format=json"] |
| 222 |
stdout = check_output(local_cmd) |
| 223 |
return loads(stdout) |