Suppose I have two separate .sls files that define the same state ID -- perhaps for something very common, like a conf file. Suppose further that they are both applied to the same minion, like so:
# salt/app1/init.sls
conf-file:
file.managed:
- name: /etc/app1.conf
- source: salt://app1/files/app1.conf.jinja
# salt/app2/init.sls
conf-file:
file.managed:
- name: /etc/app2.conf
- source: salt://app2/files/app2.conf.jinja
# salt/top.sls
base:
minion1:
- app1
- app2
Is this an error? Do state IDs need to be unique only within their own .sls file, or do they need to be unique across all files applied to a given minion?
(Context: To avoid ID collisions, I habitually prefix all state IDs with the name of the state, e.g. app1-conf-file
. While describing that pattern to a colleague, I realized that I don't know for sure if it is necessary.)
Yes, they need to be unique in the context of an execution. It means that you can have two states with the same ID if you are absolutely sure that they will never run together.
I recommend you to add a meaningful prefix to avoid any possible collision.
Reference in doc https://docs.saltstack.com/en/latest/ref/states/highstate.html#id-declaration
ID declaration
[...]
Must be unique across entire state tree. If the same ID declaration is used twice, only the first one matched will be used. All subsequent ID declarations with the same name will be ignored.
In reality this is not true, on 2019.2 it raises an error "Detected conflicting IDs, SLS IDs need to be globally unique"