You've already forked ansible-netbox-inventory
feat: ip_version option to restrict inventories to ipv4 or ipv6
inventory.ip_version (or NETBOX_INVENTORY_IP_VERSION): both (default), v4 or v6; the ip-addresses source filters by family server-side, the virtual-machines source picks primary_ip4/primary_ip6 for ansible_host.
This commit is contained in:
@@ -91,6 +91,14 @@ class TestValidateOptions:
|
||||
def test_named_without_value_is_fine(self):
|
||||
inv.validate_options("ip-addresses", "named", "")
|
||||
|
||||
def test_valid_ip_versions_pass(self):
|
||||
for ip_version in ("both", "v4", "v6"):
|
||||
inv.validate_options("ip-addresses", "", "", ip_version)
|
||||
|
||||
def test_unknown_ip_version(self):
|
||||
with pytest.raises(inv.InventoryError, match="Unknown ip_version"):
|
||||
inv.validate_options("ip-addresses", "", "", "ipv4")
|
||||
|
||||
|
||||
class TestHostEntries:
|
||||
def test_ip_with_dns_name(self):
|
||||
@@ -133,6 +141,21 @@ class TestHostEntries:
|
||||
_, host = inv.vm_host_entry({"name": "vm02"})
|
||||
assert "ansible_host" not in host
|
||||
|
||||
def test_vm_ip_version_selects_primary_ip_field(self):
|
||||
vm = {
|
||||
"name": "vm01",
|
||||
"primary_ip": {"address": "2001:db8::20/64"},
|
||||
"primary_ip4": {"address": "192.0.2.20/24"},
|
||||
"primary_ip6": {"address": "2001:db8::20/64"},
|
||||
}
|
||||
assert inv.vm_host_entry(vm, "both")[1]["ansible_host"] == "2001:db8::20"
|
||||
assert inv.vm_host_entry(vm, "v4")[1]["ansible_host"] == "192.0.2.20"
|
||||
assert inv.vm_host_entry(vm, "v6")[1]["ansible_host"] == "2001:db8::20"
|
||||
|
||||
def test_vm_without_requested_family_has_no_ansible_host(self):
|
||||
vm = {"name": "vm01", "primary_ip4": {"address": "192.0.2.20/24"}}
|
||||
assert "ansible_host" not in inv.vm_host_entry(vm, "v6")[1]
|
||||
|
||||
|
||||
class TestClientSideFilters:
|
||||
def entries(self):
|
||||
@@ -202,6 +225,15 @@ class TestLoadConfig:
|
||||
with pytest.raises(inv.InventoryError, match="url and token"):
|
||||
inv.load_config(path)
|
||||
|
||||
def test_ip_version_defaults_to_both(self, tmp_path):
|
||||
path = self.write_config(tmp_path, "netbox:\n url: https://nb.example.com\n token: abc\n")
|
||||
assert inv.load_config(path)["ip_version"] == "both"
|
||||
|
||||
def test_ip_version_env_override_is_normalised(self, tmp_path, monkeypatch):
|
||||
path = self.write_config(tmp_path, "netbox:\n url: https://nb.example.com\n token: abc\n")
|
||||
monkeypatch.setenv("NETBOX_INVENTORY_IP_VERSION", " V4 ")
|
||||
assert inv.load_config(path)["ip_version"] == "v4"
|
||||
|
||||
def test_invalid_timeout_raises(self, tmp_path):
|
||||
path = self.write_config(
|
||||
tmp_path,
|
||||
@@ -291,6 +323,20 @@ class TestBuildInventory:
|
||||
inventory = inv.build_inventory(config, make_client())
|
||||
assert inventory["netbox"]["hosts"] == ["192.0.2.11", "web.example.com"]
|
||||
|
||||
@responses.activate
|
||||
def test_ip_version_becomes_family_parameter(self):
|
||||
responses.get(
|
||||
f"{NETBOX_URL}/api/ipam/ip-addresses/",
|
||||
json={
|
||||
"next": None,
|
||||
"results": [{"address": "2001:db8::10/64", "dns_name": "v6.example.com"}],
|
||||
},
|
||||
)
|
||||
config = {"source": "ip-addresses", "filter": "", "filter_value": "", "ip_version": "v6"}
|
||||
inventory = inv.build_inventory(config, make_client())
|
||||
assert inventory["netbox"]["hosts"] == ["v6.example.com"]
|
||||
assert "family=6" in responses.calls[0].request.url
|
||||
|
||||
@responses.activate
|
||||
def test_vm_inventory_with_cluster_filter(self):
|
||||
responses.get(
|
||||
|
||||
Reference in New Issue
Block a user