Answers: <p>The answer is that you can't directly. You have to set a grain on the minions first by doing something similar to:</p>
  
  <pre><code>salt '*' cmd.run 'grep environment /etc/salt/minion | awk "{print \"environment: \" \$2}" &gt;&gt; /etc/salt/grains'
  salt '*' saltutil.sync_grains
  salt -G environment:prod cmd.run 'df -h'
  </code></pre>
  
Answers: CC BY-SA 3.0
Answers: 2017-07-11T23:12:44.230Z
Answers: 2017-07-11T23:12:44.230Z
Answers: James Shewey
Body: <p>Salt allows users to assign machines to environments by setting in /etc/salt/minion:</p>
  
  <pre><code>environment: example
  </code></pre>
  
  <p>You can then execute environments for that state with the command <code>salt '*' state.apply saltenv=example</code></p>
  
  <p>However, it appears that the same cannot be said for an arbitrary command. Running <code>salt '*' cmd.run 'df -h' saltenv=example</code> will run on all minions instead of just the minions in the "example" environment. How can I run commands for only a specified environment?</p>
  
Tags: <configuration-management><salt>
Title: Salt Stack Reporting - Executing commands based on environment
UserDisplayName: James Shewey
ThreadText: # Salt Stack Reporting - Executing commands based on environment
  
  (Asked by: username_0 on 2017-07-11)
  
  Salt allows users to assign machines to environments by setting in /etc/salt/minion:
  
  ```
  environment: example
  
  ```
  
  You can then execute environments for that state with the command `salt '*' state.apply saltenv=example`
  
  However, it appears that the same cannot be said for an arbitrary command. Running `salt '*' cmd.run 'df -h' saltenv=example` will run on all minions instead of just the minions in the "example" environment. How can I run commands for only a specified environment?
  
  **Tags:** configuration-management, salt
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-07-11. Score: 3
  
  The answer is that you can't directly. You have to set a grain on the minions first by doing something similar to:
  
  ```
  salt '*' cmd.run 'grep environment /etc/salt/minion | awk "{print \"environment: \" \$2}" >> /etc/salt/grains'
  salt '*' saltutil.sync_grains
  salt -G environment:prod cmd.run 'df -h'
  
  ```
  
  
  ---
  
  