“undefined method mask for NilClass” when assigning subnet to non-provisioning interface

**Problem:**When editing a host in Foreman, a reproducible error occurs as soon as a subnet is assigned to an additional interface.

Error message:
“Unable to save – Failed to render template ‘xyz’, undefined method mask for NilClass”

Reproduction:

  • Add a new additional interface without a subnet → saving works

  • Assign a subnet afterwards → saving fails with the error

Additionally:

  • Deleting the affected interface is also not possible (same error)

  • The issue is resolved if the host is set to Unmanaged and then back to Managed

Setup:

  • Host with multiple interfaces

  • Provisioning interface exists and remains unchanged

  • Subnet is correctly configured (network, netmask, organization, location)

  • No VLAN configured

Observation:
The error occurs during template rendering and indicates that .mask is being called on a nil object. The issue only appears when a subnet is assigned to the additional interface.

Foreman and Proxy versions: 3.18.1

Foreman and Proxy plugin versions: 3.18.1

Distribution and version: RHEL 9.7

Other relevant data:

What I believe is happening here:
Since Foreman 3.16, when updating an existing host, Foreman tries to validate that all required provisioning templates can be rendered without an error.
In this situation, I assume that the new interface is probably not completely saved/saved in time when the check for template renderability runs. There have been quite a few improvements made in this area during the last few months, but this is probably another edge case noone has discovered yet.
I assume you have some kind of custom provisioning template/snippet for network setup where you loop over the interface and call something like interface.subnet.mask on it.
The default templates that ship with Foreman avoid this situation by wrapping the access to the subnet attributes inside a if subnet style block (though this is probably a side-effect of supporting all of iv4-only/ipv6-only/dual-stack setups). In our custom snippets, we also did not realize this problem yet because we have next if !interface.managed? || interface.subnet.nil? at the start of our iteration (again, for totally unrelated reasons).
While I do think this is a bug that should get fixed and I want to encourage you to file a bug in redmine, adjusting your interface configuration to skip interfaces where subnet is nil might be a workaround in the meantime.