patternMinor
Salt Stack Reporting - Executing commands based on environment
Viewed 0 times
reportingexecutingstackcommandssaltenvironmentbased
Problem
Salt allows users to assign machines to environments by setting in /etc/salt/minion:
You can then execute environments for that state with the command
However, it appears that the same cannot be said for an arbitrary command. Running
environment: exampleYou can then execute environments for that state with the command
salt '*' state.apply saltenv=exampleHowever, 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?Solution
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'Code Snippets
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'Context
StackExchange DevOps Q#1523, answer score: 3
Revisions (0)
No revisions yet.