Saltstack 简明教程
SaltStack - Remote Execution
Salt 的核心概念之一是远程执行。Salt 可以在数秒内跨数千个系统执行命令。Salt 使用自己的命令来实现该功能。我们现在了解本章中用于远程执行的不同 Salt 命令。
One of the core concepts of Salt is remote execution. Salt can execute commands across thousands of systems in a matter of seconds. Salt uses its own command to do this functionality. Let us now understand the different Salt commands for remote execution in the chapter.
Salt Command
Salt 命令允许 Salt Master 与一个或多个 Salt minions 进行通信。基本语法如下所示,
Salt command enables the Salt master to communicate with one or more Salt minions. The basic syntax is as follows,
salt '<target>' <module.function> [arguments]
上面的命令语法包含以下三个主要组件。
The above command syntax consists of the following three main components.
-
target − It determines which systems is applied by the command.
-
module.function − It is a command. Commands consists of a module and function.
-
arguments − Additional data needed for calling the function.
让我们详细了解每个组件。
Let us understand each of the components in detail.
What is the Target Component?
目标是一个组件,它允许您过滤微件(托管系统)以运行函数。下面定义了一个使用目标组件的简单命令。
Target is a component, which allows you to filter minions (managed system) to run the function. A simple command using the target component is defined below.
salt '*' test.ping
它将生成以下 output −
It will produce the following output −
minion2:
True
minion1:
True
在这里,目标 ‘ ’* 表示所有受管理的系统。此处的 ‘ test ’ 是一个模块,而 ping 是一个函数。这用于测试远程系统中的 ping 服务。我们将在后续章节中了解不同的模块及其函数。
Here, the target ‘’* represents all the managed systems. The ‘test’ here is a module and ping is a function. This is used to test the ping service in the remote system. We will learn about the different modules and its functions in subsequent chapters.
Targets using ID (minion)
您可以使用目标中的 id 向特定微件发送命令。不要使用 ' ' , you can replace it using *minion id 。它在下面定义。
You can send a command to a specific minion using its id in the target. Instead of using '', you can replace it using *minion id. It is defined below.
salt 'minion1’ test.ping
它将生成以下 output −
It will produce the following output −
minion1:
True
Targets using Regular Expression
可以按特定正则表达式过滤目标。它在下面定义。
Targets can be filtered by specific regular expression. It is defined below.
salt -E 'minion[0-9]' test.ping
它将生成以下 output −
It will produce the following output −
minion2:
True
minion1:
True
Module and Functions (module.function)
Salt 可以执行 shell 命令;更新软件包并同时在所有托管系统中分发文件等。Salt 使用模块执行这些操作。Salt 为所有可用功能提供了特殊模块。让我们在本教程中使用一些简单的示例了解不同的 Salt 模块。
Salt can execute shell commands; update packages and distribute files, etc., in all of its managed systems simultaneously. Salt does these operations using modules. Salt has special modules for all the available functionalities. Let us understand the different Salt modules using some simple example in this chapter.
Shell Command
Salt 在多个系统上使用 ` cmd.run ` 命令来远程执行 shell 命令。` cmd ` 是主模块,` run ` 是 ` cmd ` 模块中可用的其中一个函数。` run ` 函数使任何 shell 命令都能在远程系统中执行,如下面的代码块所示。
Salt executes shell commands remotely across multiple systems using the cmd.run command. The cmd is the main module and run is one of the function available in the cmd module. The run function enables any shell command to be executed in the remote system as shown in the code block below.
salt '*' cmd.run 'ls -l /etc'
它将生成以下 output −
It will produce the following output −
minion2:
total 868
drwxr-xr-x 7 root root 4096 Jan 26 22:10 X11
drwxr-xr-x 3 root root 4096 Jan 26 21:02 acpi
-rw-r--r-- 1 root root 2981 Jan 26 20:48 adduser.conf
-rw-r--r-- 1 root root 10 Jan 26 21:04 adjtime
drwxr-xr-x 2 root root 4096 Jan 26 22:10 alternatives
drwxr-xr-x 3 root root 4096 Jan 26 20:53 apm
drwxr-xr-x 3 root root 4096 Jan 26 21:02 apparmor
drwxr-xr-x 9 root root 4096 Jan 26 21:02 apparmor.d
drwxr-xr-x 3 root root 4096 Jan 26 21:02 apport
drwxr-xr-x 6 root root 4096 Jan 29 07:14 apt
drwxr-xr-x 2 root root 4096 Jan 26 22:10 at-spi2
……………
……………
minion1:
total 868
drwxr-xr-x 7 root root 4096 Jan 26 22:10 X11
drwxr-xr-x 3 root root 4096 Jan 26 21:02 acpi
-rw-r--r-- 1 root root 2981 Jan 26 20:48 adduser.conf
-rw-r--r-- 1 root root 10 Jan 26 21:04 adjtime
drwxr-xr-x 2 root root 4096 Jan 26 22:10 alternatives
drwxr-xr-x 3 root root 4096 Jan 26 20:53 apm
drwxr-xr-x 3 root root 4096 Jan 26 21:02 apparmor
drwxr-xr-x 9 root root 4096 Jan 26 21:02 apparmor.d
drwxr-xr-x 3 root root 4096 Jan 26 21:02 apport
drwxr-xr-x 6 root root 4096 Jan 29 07:09 apt
drwxr-xr-x 2 root root 4096 Jan 26 22:10 at-spi2
-rw-r----- 1 root daemon 144 Oct 21 2013 at.deny
-rw-r--r-- 1 root root 2177 Apr 9 2014 bash.bashrc
-rw-r--r-- 1 root root 45 Mar 22 2014 bash_completion
……………
……………
Show Disk Usage
Salt 提供了一个专门模块 ` disk ` 来获取受管系统的完整磁盘详细信息。` diskmodule ` 有一个 ` usage ` 函数来查询详细信息。
Salt provides a special module, disk to get the complete disk details of the managed system. The diskmodule has a usage function to query the details.
salt '*' disk.usage
它将生成以下 output −
It will produce the following output −
minion1:
----------
/:
----------
1K-blocks:
41251136
available:
37852804
capacity:
5%
filesystem:
/dev/sda1
used:
1662420
/dev:
----------
1K-blocks:
503908
available:
503896
capacity:
1%
filesystem:
udev
used:
12
/run:
----------
1K-blocks:
101780
available:
101412
capacity:
1%
filesystem:
tmpfs
used:
368
/run/lock:
----------
1K-blocks:
5120
available:
5120
capacity:
0%
filesystem:
none
used:
0
/run/shm:
----------
1K-blocks:
508884
available:
508872
capacity:
1%
filesystem:
none
used:
12
/run/user:
----------
1K-blocks:
102400
available:
102400
capacity:
0%
filesystem:
none
used:
0
/sys/fs/cgroup:
----------
1K-blocks:
4
available:
4
capacity:
0%
filesystem:
none
used:
0
/vagrant:
----------
1K-blocks:
303114632
available:
252331440
capacity:
17%
filesystem:
none
used:
50783192
minion2:
----------
/:
----------
1K-blocks:
41251136
available:
37852804
capacity:
5%
filesystem:
/dev/sda1
used:
1662420
/dev:
----------
1K-blocks:
503908
available:
503896
capacity:
1%
filesystem:
udev
used:
12
/run:
----------
1K-blocks:
101780
available:
101412
capacity:
1%
filesystem:
tmpfs
used:
368
/run/lock:
----------
1K-blocks:
5120
available:
5120
capacity:
0%
filesystem:
none
used:
0
/run/shm:
----------
1K-blocks:
508884
available:
508872
capacity:
1%
filesystem:
none
used:
12
/run/user:
----------
1K-blocks:
102400
available:
102400
capacity:
0%
filesystem:
none
used:
0
/sys/fs/cgroup:
----------
1K-blocks:
4
available:
4
capacity:
0%
filesystem:
none
used:
0
/vagrant:
----------
1K-blocks:
303114632
available:
252331440
capacity:
17%
filesystem:
none
used:
50783192
Network Interfaces
Salt 提供了一个单独的模块、网络和函数,模块内的接口用来查询受管系统的网络接口信息。
Salt provides a separate module, network and function, interfaces inside the module to query the network interface information about the managed systems.
salt '*' network.interfaces
它将生成以下 output −
It will produce the following output −
minion1:
----------
eth0:
----------
hwaddr:
08:00:27:04:3e:28
inet:
|_
----------
address:
10.0.2.15
broadcast:
10.0.2.255
label:
eth0
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::a00:27ff:fe04:3e28
prefixlen:
64
scope:
link
up:
True
eth1:
----------
hwaddr:
08:00:27:34:10:52
inet:
|_
----------
address:
192.168.50.11
broadcast:
192.168.50.255
label:
eth1
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::a00:27ff:fe34:1052
prefixlen:
64
scope:
link
up:
True
lo:
----------
hwaddr:
00:00:00:00:00:00
inet:
|_
----------
address:
127.0.0.1
broadcast:
None
label:
lo
netmask:
255.0.0.0
inet6:
|_
----------
address:
::1
prefixlen:
128
scope:
host
up:
True
minion2:
----------
eth0:
----------
hwaddr:
08:00:27:04:3e:28
inet:
|_
----------
address:
10.0.2.15
broadcast:
10.0.2.255
label:
eth0
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::a00:27ff:fe04:3e28
prefixlen:
64
scope:
link
up:
True
eth1:
----------
hwaddr:
08:00:27:a7:31:8e
inet:
|_
----------
address:
192.168.50.12
broadcast:
192.168.50.255
label:
eth1
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::a00:27ff:fea7:318e
prefixlen:
64
scope:
link
up:
True
lo:
----------
hwaddr:
00:00:00:00:00:00
inet:
|_
----------
address:
127.0.0.1
broadcast:
None
label:
lo
netmask:
255.0.0.0
inet6:
|_
----------
address:
::1
prefixlen:
128
scope:
host
up:
True
sys.doc Execution Module
Salt 函数可以被发送到 ` sys.doc ` 执行模块。这被用来直接从命令行获取任何模块的详细信息。Salt 函数是自文档的。可以通过小 Minion 通过 sys.doc() 函数检索所有函数文档,该函数如下所述。
Salt functions can be sent to the sys.doc execution module. This is used to get the details on any module directly from the command line. The Salt functions are self-documenting. All the function documentation can be retrieved from the minions via the sys.doc() function, which is defined below.
salt '*' sys.doc
Arguments for a Function Call
参数用来为函数调用提供附加数据。下面给出简单参数示例。
Arguments are used to provide additional data for the function call. A simple argument example is given below.
salt '*' sys.doc pkg.install
此处,` pkg.install ` 参数是安装特定软件包的模块。
Here, the argument pkg.install is a module to install specific packages.
Python Function
参数是函数的空间分隔参数。它允许 python 代码被传递为参数,如下所示。
Arguments are space-delimited parameters to the function. It allows the python code to be passed as arguments as given below.
salt '*' cmd.exec_code python 'import sys;print sys.version'
它将生成以下 output −
It will produce the following output −
minion2:
2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4]
minion1:
2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4]
同样,您也可以使用可选关键字和 ` YAML ` 格式。
Similarly, you can use optional keywords and the YAML format as well.