Skip to content

Commit 1fa275f

Browse files
committed
#2683 - Syntax to include states from explicit env, or resolve with _xenv.
1 parent 4b6dc3b commit 1fa275f

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

salt/state.py

+40-18
Original file line numberDiff line numberDiff line change
@@ -1473,19 +1473,37 @@ def render_state(self, sls, env, mods, matches):
14731473
errors.append(err)
14741474
else:
14751475
for inc_sls in state.pop('include'):
1476-
# Subset of my_avail containing the include sls
1477-
my_env = [
1478-
aenv for aenv in matches
1479-
if fnmatch.filter(self.avail[aenv], inc_sls)
1480-
]
1481-
1482-
# An include must only be one available in one environment
1483-
# Or the include must exist in the current environment
1484-
if len(my_env) == 1 or env in my_env:
1476+
# inc_sls may take the form of:
1477+
# 'sls.to.include' <- same as {<env>: 'sls.to.include'}
1478+
# {<env_key>: 'sls.to.include'}
1479+
# {'_xenv': 'sls.to.resolve'}
1480+
XENV_KEY = '_xenv'
1481+
1482+
if isinstance(inc_sls, dict):
1483+
env_key, inc_sls = inc_sls.popitem()
1484+
else:
1485+
env_key = env
1486+
1487+
if env_key != XENV_KEY:
1488+
# Resolve inc_sls in the specified environment
1489+
if env_key in matches and fnmatch.filter(self.avail[env_key], inc_sls):
1490+
resolved_envs = [env_key]
1491+
else:
1492+
resolved_envs = []
1493+
else:
1494+
# Resolve inc_sls in the subset of environment matches
1495+
resolved_envs = [
1496+
aenv for aenv in matches
1497+
if fnmatch.filter(self.avail[aenv], inc_sls)
1498+
]
1499+
1500+
# An include must be resolved to a single environment, or
1501+
# the include must exist in the current environment
1502+
if len(resolved_envs) == 1 or env in resolved_envs:
14851503
if inc_sls not in mods:
14861504
nstate, mods, err = self.render_state(
14871505
inc_sls,
1488-
my_env[0] if len(my_env) == 1 else env,
1506+
resolved_envs[0] if len(resolved_envs) == 1 else env,
14891507
mods,
14901508
matches
14911509
)
@@ -1495,14 +1513,18 @@ def render_state(self, sls, env, mods, matches):
14951513
errors += err
14961514
else:
14971515
msg = ''
1498-
if not my_env:
1499-
msg = ('Unknown include: Specified SLS {0} is not available on the salt master '
1500-
'in any available environments {1} '
1501-
).format(inc_sls, ', '.join(matches))
1502-
elif len(my_env) > 1:
1503-
msg = ('Ambiguous include: Specified SLS {0} is available on the salt master '
1504-
'in available environments {1}'
1505-
).format(inc_sls, ', '.join(my_env))
1516+
if not resolved_envs:
1517+
msg = ('Unknown include: Specified SLS {0}: {1} is not available on the salt '
1518+
'master in environment(s): {2} '
1519+
).format(env_key,
1520+
inc_sls,
1521+
', '.join(matches) if env_key == XENV_KEY else env_key)
1522+
elif len(resolved_envs) > 1:
1523+
msg = ('Ambiguous include: Specified SLS {0}: {1} is available on the salt master '
1524+
'in multiple available environments: {2}'
1525+
).format(env_key,
1526+
inc_sls,
1527+
', '.join(resolved_envs))
15061528
log.error(msg)
15071529
if self.opts['failhard']:
15081530
errors.append(msg)

0 commit comments

Comments
 (0)