Hosts and groups are always ‘global’. If you define a host or group more than once under different ‘branches’ or ‘instances’, the host or group remains the same entity. Defining a host or group more than once either adds new information to it or overwrites any conflicting information with the latest definition.
Does that mean that each host and group names must be “completely” unique ?
Yes, inventory host names and host group names are unique.
It’s confusing (at first) because there’s so much flexibility in how you can define groups in hierarchies. But those hierarchies are for your benefit; they don’t actually exist as far as a running play is concerned. Once your inventories are parsed, refined, augmented, merged, and presented to a play, there are only two things left:
a dict where all the host names are the keys, and each value is a flat list containing all the groups the keying host is a member of, and
a dict where all the keys are the group names, and each value is a list containing all the hosts that are members of the keying group.
That’s it! Host groups are “flat” – i.e. they don’t “contain” sub-groups, and each group name is unique. Any group hierarchies exist only in our imaginations.
I’m either missing a point or being insufferably pedantic, but I find the “must be unique” phrase confusing. Names of groups (and hosts) are unique, regardless of the author’s intent. (See this discussion from last month for example.) A given string can be used as a group name (or host name) any number of times in a single or multiple inventory sources; each such instance contributes to the definition of a single group (or host).
It’s my fault. I probably should have been more specific.
In hindsight, I think this question was a bit “silly” (it often is like that when you’re starting out), but if I had to rephrase the question, it would be: Can a group name have the same name as a host? Or, in other words, is Ansible able to distinguish between a group and a host?
From what I understood from your first answer: when Ansible parses inventory files, it creates two dictionaries: one for groups and the other for hosts. So I’m beginning to think that it’s still possible to give the same name to a host and a group…
Finally, I think this is a bit of a silly question in that I don’t think it’s a good idea to give the same name to a group and a host, for obvious reasons.
FYI: The host/group must be unique within the same inventory. You can have the same group name/hostname in multiple inventories.
I’m not sure I understand correctly. Do you mean that the same names can be reused between different inventories? By different, I mean “independent”/“unrelated” inventories.
“Inventory” is one of those overloaded words that means whatever you’re thinking about at the time. If you want to be precise - or at least understood - it’s probably better to specify either “inventory source(s)” or the “active” or “loaded” inventory. Inventory sources are .ini or .yaml files with static inventory definitions, or scripts that produce inventory definitions with varying degrees if dynamism, and probably a few others. In one sense, they are all independent — until they get loaded by a running Ansible process. At that point they are combined pretty much as if they had all been one inventory source to start with. If any of them have host or group definitions with the same name, they cumulatively contribute to the final named host or group that a playbook sees.
The plain and simple answer is that you cannot use the same name for a group and a host. The reason is that Ansible host patterns, for example in a hosts: key of the playbook:
- hosts: somegroup,somehost
...
accepts both group names and host names including complex expressions with them. If there was a group and a host with the same name, Ansible would not be able to tell if you are referring to a group or to a host.
Under the hood, the list of hosts and groups are effectively held in separate lists but when you use an identifier in a host pattern, Ansible will search both group and host lists for that unique identifier.
Additionally, hosts names can contain dash (-) and period (.) as those are allowed characters for host names in general (think DNS). Group names are more restrictive and are expected to follow Python variable naming rules (alphanumerics and underscore). If you use dash in group name, you will get a warning.