Table of contents
1.
Introduction
2.
Remote Execution in Salt
3.
Giving Orders To The Minions
3.1.
Setting the Targets
3.2.
Defining the Function 
3.3.
Giving the Arguments
4.
Giving Commands To The Salt Minions
5.
The Salt Command
5.1.
Targetting the Minions
5.2.
Calling The Functions
5.3.
Executing The Compound Command
6.
Completion of CLI
7.
Frequently Asked Questions
7.1.
What is Jinja in DBT?
7.2.
Is Jinja2 a library?
7.3.
What is the difference between Jinja and Jinja2?
7.4.
What is Jinja salt?
7.5.
What is Salt software used for?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

Remote Execution in Salt

Author Amit Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Have you worked on the deployment of complex IT Systems? Have you ever worked on the automation of the configuration of IT Systems?

title

This article focuses on one of the automation engines, i.e., Salt Project. We will study remote execution in detail. We will try to understand how to give orders to minions as well. We will also see how to find the target minion, compound command execution, and many other concepts.

Remote Execution in Salt

Remote execution is one of any deployment tool's best and most important features. Let's try to understand the remote execution feature with a tutorial.

But before we move on, you must have salt installed in your system. You must have already configured it as well. 

Giving Orders To The Minions

For the remote execution, you must have a master and a minimum of one minion. They will need to communicate with each other. You can use the salt command to perform any action on the minion. 

A Salt call is composed of three main components:

salt   '<target_minion>'   <function_to_perform>   [arguments_for_the_function]

Setting the Targets

The target component handles the task of filtering minions. It helps you to find the minion that should perform the function you want to perform. A glob on the minion id is the default filter. For instance,

salt '*' test.version

 

salt '*.test.org' test.version

 

You can also use the Grains system to target the minions based on the minion system information.

salt -G 'os:Windows' test.version

 

You can also filter the target using the "regular expressions."

salt -E 'virtmach[0-9]' test.version

 

You can specify the targets that you want to use in a list as well.

salt -L 'sam,bell,bach,que' test.version

 

You can also specify and combine multiple target types in a single command. 

salt -C 'G@os:Windows and webser* or E@database.*' test.version

Defining the Function 

A function is like a task that you want to perform. It is a functionality provided by a module. Salt has quite a big collection of functions that you can use without any difficulty. You can check the list of all the available functions on the minion using the following command.

salt '*' sys.doc

 

Let's see some examples of the functions.

In case you want to see all the minions that are currently available, you can use the following command.

salt '*' test.version

 

In case you want to run a shell command that is an arbitrary command, you can use the following command.

salt '*' cmd.run 'uname -a'

Giving the Arguments

The arguments are like parameters that are given to a function. These are generally delimited by space. For example,

salt '*' cmd.exec_code python 'import sys; print sys.version'

 

You can also use keywords based arguments, but they are completely optional.

salt '*' pip.install salt timeout=5 upgrade=True

 

These arguments are in the form of kwarg=argument.

Giving Commands To The Salt Minions

The root user on the Salt master has access to a command-line client through which Salt can be managed. The Salt client API is used by the Salt command-line client to interact with the Salt master server. The Salt client is uncomplicated and easy to use.

Commands may be readily given to the minions by using the Salt client.

Each of these commands allows an explicit --config option to specify the configuration file for the master or the minion. The environment variables SALT_MASTER_CONFIG and SALT_MINION_CONFIG are used as a fallback if this option is not specified and the default configuration file is missing.

The Salt Command

You can use the Salt command to send information to your Salt minions. But Salt commands require some components. You must define the target minions, the function you want to call and the parameters or argument that function needs.

Targetting the Minions

The first argument that you will pass to the Salt will define the minions that you want to target. You can access the minions that you want to target only by their hostname. A bash glob is the default target type.

salt '*codingninjas.com' sys.doc

 

You can also define the minions you want to target using regular expressions.

salt -E '.*' cmd.run 'ls -l | grep cn'

 

You can also define a list of the hosts as well and send it to Salt.

salt -L too.bach.bas,que.qua cmd.run 'ps aux | grep cn'

Calling The Functions

After you have specified the target, you must call the function that you want to run on the target.

You can use space-delimited arguments in the functions.

salt '*' cmd.exec_code python 'import sys; print sys.version'

 

You can also use keyword arguments in the functions.

salt '*' pip.install salt timeout=5 upgrade=True

 

These arguments are in the form of kwarg=argument.

The arguments are in the form of YAML:

salt '*' cmd.run 'echo "Heyy: $FIRST_NAME"' env='{FIRST_NAME: "Ken"}'

 

Keep in mind that it is mandatory for dictionaries to have curly braces around them.

You can use the test.arg_repr command to test what parameters you have passed to a specific module. 

salt '*' test.arg_repr 'echo "Heyy: $FIRST_NAME"' env='{FIRST_NAME: "Ken"}'

 

Looking For Minion Functions

All the Salt functions have all the information about them. You can use the sys.doc() function to check all the documentation related to a function.

salt '*' sys.doc

Executing The Compound Command

You can also send multiple commands in a single publish in case you want to send a number of commands to a single target that you have specified. This reduces the stress on your network and increases the rate of information retrieval.

Instead of providing a single function and argument, a list of functions and arguments is sent when a compound command is executed. The minion runs the functions in the sequence specified on the command line, and the results are then returned in the form of a dictionary. In other words, the set of commands is called in a predictable manner, and the data that is returned can be understood quite easily.

Let's check an example in which you can execute compound commands by passing a list of functions that are delimited by commas. This list is also followed by a list of arguments as well that are also delimited.

salt '*' cmd.run,test.ping,test.echo 'cat /proc/cpuinfo',,too

 

The catch here is that there must be a placeholder for the arguments that are missing if a function receives no arguments. Due to this, two commas are placed directly next to one another in the example above. Since test.ping does not accept arguments, we must add an additional comma to prevent Salt from attempting to send "foo" to test.ping.

You will have to add spaces around the commas in case you require to pass arguments with some commas. For instance,

salt '*' cmd.run,test.ping,test.echo 'echo "3,2,1"' , , too

 

You can use the --args-separator option to modify the arguments separator.

salt --args-separator=:: '*' some.fun,test.echo params with , comma :: too

Completion of CLI

You can use the Shell completion scripts for the Salt CLI as well. They are provided in the pkg Salt source directory.

Frequently Asked Questions

What is Jinja in DBT?

In the data build tool (dbt), SQL can be combined with Jinja. Jinja is a templating language. Your dbt project into a programming environment using Jinja for SQL. It allows you to do things that aren't usually possible in SQL. For example, with Jinja, you can use control statements in SQL.

Is Jinja2 a library?

Jinja is a library for Python. It is designed to be fast, flexible, and secure. Suppose you have worked with other text-based template languages, such as Django or Smarty. Then you will find it super easy to work with.

What is the difference between Jinja and Jinja2?

Jinja 2 has a similar syntax as Jinja 1. The difference is that around the argument list, macros now require parentheses. Also, Jinja 2 allows dynamic inheritance and dynamic includes. The old helper function rendertemplate is invalid now and included used instead.

What is Jinja salt?

Jinja is the default templating language. Jinja supports a secure, sandboxed template execution environment. It is the advantage used by Salt. Salt highly recommends the usage of jinja/jinja |yaml files.

What is Salt software used for?

SALT stands for Systematic Analysis of Language Transcripts. It is software that standardizes the process of transcribing, eliciting, and analyzing language samples. It includes standard reports, a transcription editor, and reference databases for comparison with typical peers.

Conclusion

In this article, we have studied one of the automation engines, i.e., Salt Project. We have studied the remote execution in Salt in detail. We have also tried to understand how to target the minions in Salt and define the function as well.

We hope that this article has provided you with the help to enhance your knowledge regarding the Salt Project and if you would like to learn more, check out our articles on salt-mine and salt-event-system.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Live masterclass