How to set specific location based on subnets?

Problem: When discovery finds hosts, always Default Location is set. Example:

Expected outcome: I would like to set the locations based on subnets. Is this possible?

Foreman and Proxy versions: 1.22

Foreman and Proxy plugin versions: 1.22

Other relevant data:
I defined the relevant subnets in the location definitions but the discovery seems to ignore that.
Can you give me a hint?

From our manual:

3.1.3 Organizations and Locations

If Locations and/or Organizations are enabled, Foreman assigns Organization and Location to discovered hosts according to the following rules from top to bottom:

  • According to the discovery_organization or discovery_location , if present. These can be set under Administer > Settings > Discovered .
  • If foreman_organization or foreman_location fact is present, set accordingly. Fact names which are looked up can be configured in Administer > Settings > Puppet section as Organization/Location fact setting.
  • If Subnet was determined for particular discovered host, use the first Organization and Location associated with the Subnet.
  • Select the first Organization / Location ordered by title (name the org/loc was created with). This is a workaround until we fix provisioning of discovered hosts without taxonomy.

Organization or Location can be changed via the “bulk actions” menu which appears once once or more discovered hosts are selected.

This is what’s currently implemented. It’s not ideal, but it is what it is.

I found the issue. It seems the foreman_location fact is set to Default Location. That’s why the subnet setting is not working.
But where does Foreman get this value from? The default is “not set”.

grafik

Foreman discovery ignores this setting from version 15.0. Bug #26100: Remove foreman_organization/location from taxonomy detection and add tests - Discovery - Foreman

Let me update the documentation then:

3.1.3 Organizations and Locations

If Locations and/or Organizations are enabled, Foreman assigns Organization and Location to discovered hosts according to the following rules from top to bottom:

  • According to the discovery_organization or discovery_location , if present. These can be set under Administer > Settings > Discovered .
  • If Subnet was determined for particular discovered host, use the first Organization and Location associated with the Subnet.
  • Select the first Organization / Location ordered by title (name the org/loc was created with). This is a workaround until we fix provisioning of discovered hosts without taxonomy.

Here is the relevant code:

Very likely you have this location set to the subnet which is detected, therefore it pick it. Also note that it picks “first” location. This is lame, if you have multiple locations it will pick “random” location (!). I know, this is terrible. Proper solution requires UI/CLI/API changes, I’d probably delay this until we merge discovery into core which we are considering.