From f6aeae1abd5cd774be13c4f596e915e135d9024d Mon Sep 17 00:00:00 2001 From: Claudio Luck <870386+cluck@users.noreply.github.com> Date: Tue, 19 Aug 2025 22:46:38 +0200 Subject: [PATCH] Handle IP addresses configured on disappeared interfaces (#391) * Handle IP addresses configured on disappeared interfaces * Handle IP addresses configured on disappeared interfaces Always set type property, add changelog fragment * Handle IP addresses configured on disappeared interfaces (wording) Co-authored-by: Felix Fontein --------- Co-authored-by: Claudio Luck Co-authored-by: Felix Fontein --- .../fragments/391-report-unknown-interfaces.yml | 12 ++++++++++++ plugins/modules/api_facts.py | 8 +++++--- 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/391-report-unknown-interfaces.yml diff --git a/changelogs/fragments/391-report-unknown-interfaces.yml b/changelogs/fragments/391-report-unknown-interfaces.yml new file mode 100644 index 0000000..bcef417 --- /dev/null +++ b/changelogs/fragments/391-report-unknown-interfaces.yml @@ -0,0 +1,12 @@ +--- +bugfixes: + - | + api_facts - also report interfaces that are inferred only by reference by IP addresses. + RouterOS's APIs have IPv4 and IPv6 addresses point at interfaces by their name, which can + change over time and in-between API calls, such that interfaces may have been enumerated + under another name, or not at all (for example when removed). Such interfaces are now reported + under their new or temporary name and with a synthetic ``type`` property set to differentiate + the more likely and positively confirmed removal case (with ``type: "ansible:unknown"``) from + the unlikely and probably transient naming mismatch (with ``type: "ansible:mismatch"``). + Previously, the api_facts module would have crashed with a ``KeyError`` exception + (https://github.com/ansible-collections/community.routeros/pull/391). diff --git a/plugins/modules/api_facts.py b/plugins/modules/api_facts.py index 09c0fbb..d0f6b00 100644 --- a/plugins/modules/api_facts.py +++ b/plugins/modules/api_facts.py @@ -317,8 +317,10 @@ class Interfaces(FactsBase): def populate_addresses(self, data, family): for value in data: key = value['interface'] - if family not in self.facts['interfaces'][key]: - self.facts['interfaces'][key][family] = [] + iface = self.facts['interfaces'].setdefault(key, ( + {"type": "ansible:unknown"} if key.startswith('*') else + {"type": "ansible:mismatch"})) + iface_addrs = iface.setdefault(family, []) addr, subnet = value['address'].split('/') subnet = subnet.strip() # Try to convert subnet to an integer @@ -328,7 +330,7 @@ class Interfaces(FactsBase): pass ip = dict(address=addr.strip(), subnet=subnet) self.add_ip_address(addr.strip(), family) - self.facts['interfaces'][key][family].append(ip) + iface_addrs.append(ip) def add_ip_address(self, address, family): if family == 'ipv4':