How to use the command 'salt-call' (with examples)
Salt-call is a command-line tool used in SaltStack to invoke salt functionality on a minion. It allows for the execution of various Salt states and functions on a single minion locally. This article will illustrate several use cases of the ‘salt-call’ command.
Use case 1: Perform a highstate on this minion
Code:
salt-call state.highstate
Motivation: By running the ‘state.highstate’ command, you can apply the highstate, which includes all the states defined on the minion. This is useful when you want to ensure that the minion is in the desired state and all the necessary configurations and packages are correctly applied.
Explanation:
- ‘salt-call’: The command to invoke Salt functionality on a minion.
- ‘state.highstate’: The specific state function to execute the highstate.
Example Output:
local:
----------
ID: <state_id1>
Function: <module.function1>
Result: True
Comment: <state_comment1>
Started: <datetime>
Duration: <duration>
...
Summary for local
------------
Succeeded: <num>
Failed: <num>
Use case 2: Perform a highstate dry-run
Code:
salt-call state.highstate test=true
Motivation: A dry-run allows you to preview the changes that would be made during a highstate without actually applying them. This is helpful for verifying the expected changes before actually executing the highstate.
Explanation:
- ’test=true’: This argument is used to enable the test mode, where the highstate is simulated but not executed. It provides a preview of the changes without actually performing them.
Example Output:
local:
----------
ID: <state_id1>
Function: <module.function1>
Result: None
Comment: Would have made changes...
Started: <datetime>
Duration: <duration>
...
Summary for local
------------
Succeeded: 0
Failed: 0
Use case 3: Perform a highstate with verbose debugging output
Code:
salt-call -l debug state.highstate
Motivation: When debugging Salt states or troubleshooting issues, it can be helpful to get detailed verbose output. By specifying the ‘-l debug’ argument, you can enable debug-level logging for the highstate execution.
Explanation:
- ‘-l debug’: This argument sets the logging level to debug, providing more detailed output for debugging purposes.
Example Output:
[DEBUG ] LazyLoaded file.recurse
...
[DEBUG ] LazyLoaded xxxxxxxxxxxxxxxxxxxxxxx
...
[DEBUG ] LazyLoaded xxxxxxxxxxxxxxxxxxxxxxxx
...
[INFO ] Executing command 'state.highstate' in directory '<path>'
<state_id1>:
----------
ID: <state_id1>
Function: <module.function1>
Name: <name>
Result: True
Comment: <comment>
Started: <datetime>
Duration: <duration>
Summary for local
------------
Succeeded: <num>
Failed: 0
Use case 4: List this minion’s grains
Code:
salt-call grains.items
Motivation: Grains are the system properties or variables collected by SaltStack. By using the ‘grains.items’ command, you can retrieve a list of all the available grains on a specific minion. This is useful when you need to gather system information or configure states based on specific minion properties.
Explanation:
- ‘grains.items’: This command retrieves all the grains available on the minion and lists them.
Example Output:
local:
----------
grain1: value1
grain2: value2
grain3: value3
...
Conclusion:
The ‘salt-call’ command is a versatile tool that allows for the execution of Salt states and functions locally on a minion. By using this command, you can perform actions like applying the highstate, previewing changes, enabling verbose debugging, and retrieving grains. These use cases demonstrate the flexibility and power of ‘salt-call’ in SaltStack deployments.