How to Execute a Single State from an SLS File in SaltStack (2015.8.3 Beryllium)


16 views

When working with SaltStack's state system, you'll often need to execute just one specific state from an SLS file rather than the entire file. This becomes particularly important when:

  • Debugging individual states
  • Testing specific configurations
  • Minimizing execution scope in production

Your current working commands show you're comfortable with basic SaltStack operations:

# Executes all states in monitoring SLS
salt-ssh w123 state.sls monitoring

# Shows the parsed SLS data
salt-ssh w123 state.show_sls monitoring

The errors you encountered reveal common misconceptions:

# Fails because state.sls_id expects a different format
salt-ssh w123 state.sls_id monitoring_packages monitoring

# Fails due to incorrect argument structure
salt-ssh w123 state.single monitoring.monitoring_packages

For SaltStack 2015.8.3 (Beryllium), here are the proper ways to execute a single state:

Method 1: Using state.sls with state ID

salt-ssh w123 state.sls monitoring saltenv=base test=True \
  exclude='*' include='monitoring_packages'

Method 2: The state.single Approach

For a more direct approach:

salt-ssh w123 state.single pkg.installed name='monitoring_packages' \
  pkgs='["python-psutil"]' order=10000

Method 3: Using state.apply (alternative)

salt-ssh w123 state.apply monitoring.monitoring_packages
  • Remember that state IDs in SLS files must be unique
  • Ensure your SLS file structure matches the module.function format
  • Be aware of Salt version differences in state execution methods

Suppose you have this monitoring/packages.sls:

monitoring_packages:
  pkg.installed:
    - pkgs:
      - python-psutil
    - order: 10000

To run just this state:

salt-ssh minion1 state.sls monitoring.packages saltenv=base \
  exclude='*' include='monitoring_packages'

Before executing, verify with:

salt-ssh w123 state.show_low_sls monitoring.packages

When working with SaltStack's state system, you might encounter situations where executing an entire SLS file is overkill, and you only need to run a specific state. The standard state.sls execution works perfectly:

salt-ssh w123 state.sls monitoring

But what if you need to target just the monitoring_packages state from this SLS file?

Many users attempt these approaches that don't work as expected:

# Attempt 1 - Incorrect state.sls_id usage
salt-ssh w123 state.sls_id monitoring_packages monitoring

# Attempt 2 - Missing arguments in state.single
salt-ssh w123 state.single monitoring.monitoring_packages

These failures typically occur because of either incorrect syntax or misunderstanding the required arguments.

For SaltStack 2015.8.3 (Beryllium) and similar versions, here's the proper way to execute a single state:

salt-ssh w123 state.single pkg.installed name=python-psutil saltenv=base

However, if you need to specifically reference the state ID from your SLS file, use:

salt-ssh w123 state.sls monitoring saltenv=base pillar='{"exclude": "*", "include": "monitoring_packages"}'

The key elements here are:

  • saltenv=base specifies the environment
  • The pillar inclusion/exclusion pattern controls which states execute
  • This method works even when direct state ID targeting fails

If you're using a more recent SaltStack version (2016.11+), this cleaner approach works:

salt-ssh w123 state.apply monitoring pillar='{"exclude": "*", "include": "monitoring_packages"}'

Always verify your state targeting with:

salt-ssh w123 state.show_sls monitoring

This helps confirm the exact state IDs available in your SLS file before attempting targeted execution.