That is not possible with checkmk means in the GUI.
There is a rule to attach host labels to hosts, but you would have to create N rules for N environments to set N labels, and more or less hardcode the environments, for example:
Hosts with name (regex) ^[^.]+\.[^.]+\.env-1\.domain\.com get the label environment:env-1
Hosts with name (regex) ^[^.]+\.[^.]+\.env-2\.domain\.com get the label environment:env-2
Hosts with name (regex) ^[^.]+\.[^.]+\.env-3\.domain\.com get the label environment:env-3
and so on.
While this possible, it is not really feasable because it does not take new environments into account and is very error prone.
Another possibility would be to use the REST-API and some Python script to automate that process.
If all your hosts are “regular” hosts with the checkmk agent installed (i.e. no SNMP devices), then it might be the easiest to put a file on each host with its own environment label. Just put a file similar to the following in the directory /var/lib/check_mk_agent/spool on the host(s). The actual filename doesn’t matter (as long as it does not start with a number), e.g.
So if i treat all of this right it would be possible to write an agent-plugin to be distributed via the agent-bakery analysing something on the host and create an output using this data simmilar to:
You can either put that section in a spool file (for example when the host is rolled out with Ansible or something similar) or you can write an agent plugin that outputs this section.
Depending on how expensive it is to “analyse something on the host” you might want to run this agent plugin only every few hours or so instead of every minute.