SaltStack常用的模块
文章目录
- 2.4.1 state.show_highstate
- 2.4.2 state.highstate
- 2.4.3 state.show_state_usage
- 2.4.4 state.show_top
- 2.4.5 state.top
- 2.4.6 state.show_sls
- 2.6.1 file.access
- 2.6.2 file.append
- 2.6.3 file.basename
- 2.6.4 file.dirname
- 2.6.5 file.check_hash
- 2.6.6 file.chattr
- 2.6.7 file.chown
- 2.6.8 file.copy
- 2.6.9 file.ditectory_exists
- 2.6.10 file.diskusage
2.4 SaltStack常用模块之state
2.4.1 state.show_highstate
显示当前系统中有哪些高级状态
[root@master ~]# salt '*' state.show_highstate
192.168.69.202:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.apache
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
apache-service:
----------
__env__:
base
__sls__:
web.apache.apache
service:
|_
----------
name:
httpd
|_
----------
enable:
True
- running
|_
----------
order:
10001
2.4.2 state.highstate
执行高级状态
[root@master ~]# salt '*' state.highstate web.apache.apache
192.168.69.202:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 20:07:37.012733
Duration: 1050.73 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 20:07:38.065796
Duration: 94.747 ms
Changes:
Summary for 192.168.69.202
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 1.145 s
2.4.3 state.show_state_usage
显示当前系统中的高级状态执行情况
[root@master ~]# salt '*' state.show_state_usage
192.168.69.202:
----------
base:
----------
count_all:
2
count_unused:
1
count_used:
1
unused:
- top
used:
- web.apache.apache
dev:
----------
count_all:
0
count_unused:
0
count_used:
0
unused:
used:
prod:
----------
count_all:
0
count_unused:
0
count_used:
0
unused:
used:
test:
----------
count_all:
0
count_unused:
0
count_used:
0
unused:
used:
2.4.4 state.show_top
返回minion将用于highstate的顶级数据
[root@master ~]# salt '*' state.show_top
192.168.69.202:
----------
base:
- web.apache.apache
2.4.5 state.top
执行指定的top file,而不是默认的
[root@master ~]# salt '*' state.top runtime.sls
192.168.69.202:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 20:17:20.129390
Duration: 917.659 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 20:17:21.048144
Duration: 46.958 ms
Changes:
Summary for 192.168.69.202
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 964.617 ms
2.4.6 state.show_sls
显示 master 上特定sls或sls文件列表中的状态数据
[root@master ~]# salt '*' state.show_sls web.apache.apache
192.168.69.202:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.apache
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
apache-service:
----------
__env__:
base
__sls__:
web.apache.apache
service:
|_
----------
name:
httpd
|_
----------
enable:
True
- running
|_
----------
order:
10001
2.5 SaltStack常用模块之salt-cp
salt-cp能够很方便的把 master 上的文件批量传到 minion上
//拷贝单个文件到目标主机的/usr/src目录下
[root@master ~]# salt '*' cmd.run 'ls /usr/src/'
192.168.69.202:
debug
kernels
[root@master ~]# salt-cp '*' /etc/passwd /usr/src/
192.168.69.202:
----------
/usr/src/passwd:
True
[root@master ~]# salt '*' cmd.run 'ls /usr/src'
192.168.69.202:
debug
kernels
passwd
//拷贝多个文件到目标主机的/usr/src目录下
[root@master ~]# salt-cp '*' /etc/shadow /etc/group /usr/src
192.168.69.202:
----------
/usr/src/group:
True
/usr/src/shadow:
True
[root@master ~]# salt '*' cmd.run 'ls /usr/src'
192.168.69.202:
debug
group
kernels
passwd
shadow
2.6 SaltStack常用模块之file
2.6.1 file.access
检查指定路径是否存在
[root@master ~]# salt '*' cmd.run 'ls /usr/src'
192.168.69.202:
debug
group
kernels
passwd
shadow
zabbix-4.0.3
[root@master ~]# salt '*' file.access /usr/src/passwd f
192.168.69.202:
True
[root@master ~]# salt '*' file.access /usr/src/abc f
192.168.69.202:
False
检查指定文件的权限信息
[root@master ~]# salt '*' cmd.run 'ls -l /usr/src/'
192.168.69.202:
total 12
drwxr-xr-x. 2 root root 6 Nov 5 2016 debug
-rw-r--r--. 1 root root 504 Feb 23 16:06 group
drwxr-xr-x. 2 root root 6 Nov 5 2016 kernels
-rw-r--r--. 1 root root 1035 Feb 23 16:05 passwd
-rw-r--r--. 1 root root 679 Feb 23 16:06 shadow
drwxr-xr-x. 13 root root 208 Feb 23 16:13 zabbix-4.0.3
[root@master ~]# salt '*' file.access /usr/src/passwd r //是否有读权限
192.168.69.202:
True
[root@master ~]# salt '*' file.access /usr/src/passwd w //是否有写权限
192.168.69.202:
True
[root@master ~]# salt '*' file.access /usr/src/passwd x //是否有执行权限
192.168.69.202:
False
2.6.2 file.append
往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
192.168.69.202:
-rw-r--r--. 1 root root 0 Feb 23 16:46 /root/a
[root@master ~]# salt '*' file.append /root/a "hello world" "haha" "xixi"
192.168.69.202:
Wrote 3 lines to "/root/a"
[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
192.168.69.202:
-rw-r--r--. 1 root root 22 Feb 23 16:51 /root/a
[root@master ~]# salt '*' cmd.run 'cat /root/a'
192.168.69.202:
hello world
haha
xixi
2.6.3 file.basename
获取指定路径的基名
[root@master ~]# salt '*' file.basename '/root/zabbix/abc'
192.168.69.202:
abc
2.6.4 file.dirname
获取指定路径的目录名
[root@master ~]# salt '*' file.dirname '/root/zabbix/abc'
192.168.69.202:
/root/zabbix
2.6.5 file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt '*' cmd.run 'md5sum /etc/passwd'
192.168.69.202:
83c9a0ffa6c002fb45a0c37a767f55f8 /etc/passwd
[root@master ~]# salt '*' file.check_hash /etc/passwd 83c9a0ffa6c002fb45a0c37a767f55f8
192.168.69.202:
True
2.6.6 file.chattr
给指定文件添加属性
//查看当前属性
[root@master ~]# salt '*' cmd.run 'lsattr /root'
192.168.69.202:
---------------- /root/anaconda-ks.cfg
---------------- /root/a
//添加属性
[root@master ~]# salt '*' file.chattr /root/a operator=add attributes=ai
192.168.69.202:
True
[root@master ~]# salt '*' cmd.run 'lsattr /root'
192.168.69.202:
---------------- /root/anaconda-ks.cfg
----ia---------- /root/a
给指定文件去除属性
[root@master ~]# salt '*' file.chattr /root/a operator=remove attributes=i
192.168.69.202:
True
[root@master ~]# salt '*' cmd.run 'lsattr /root'
192.168.69.202:
---------------- /root/anaconda-ks.cfg
-----a---------- /root/a
2.6.7 file.chown
设置指定文件的属主、属组信息
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.69.202:
total 8
-r--------. 1 tom tom 22 Feb 23 16:51 a
-rw-------. 1 root root 1259 Jan 8 15:06 anaconda-ks.cfg
[root@master ~]# salt '*' file.chown /root/a root root
192.168.69.202:
None
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.69.202:
total 8
-r--------. 1 root root 22 Feb 23 16:51 a
-rw-------. 1 root root 1259 Jan 8 15:06 anaconda-ks.cfg
2.6.8 file.copy
在远程主机上复制文件或目录
拷贝文件
[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.69.202:
total 8
-rw-r--r--. 1 root root 22 Feb 23 16:51 a
-rw-------. 1 root root 1259 Jan 8 15:06 anaconda-ks.cfg
[root@master ~]# salt '*' file.copy /root/a /root/cc
192.168.69.202:
True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.69.202:
total 12
-rw-r--r--. 1 root root 22 Feb 23 16:51 a
-rw-------. 1 root root 1259 Jan 8 15:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 22 Feb 24 14:57 cc
覆盖并拷贝目录,将会覆盖同名文件或目录
[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.69.202:
total 12
-rw-r--r--. 1 root root 22 Feb 23 16:51 a
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 22 Feb 24 14:57 cc
[root@master ~]# salt '*' file.copy /tmp/ /root/abc recurse=True
192.168.69.202:
True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.69.202:
total 16
-rw-r--r--. 1 root root 22 Feb 23 16:51 a
drwxrwxrwt. 12 root root 4096 Feb 24 15:02 abc
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 22 Feb 24 14:57 cc
删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@master ~]# salt '*' cmd.run 'ls -l /root/abc'
192.168.69.202:
total 336
drwxr-xr-x. 2 root root 6 Feb 24 15:02 abc
-rw-r--r--. 1 root root 1259 Feb 24 15:02 anaconda-ks.cfg
-rw-r--r--. 1 root root 219068 Feb 24 15:02 libevent-2.0.21-4.el7.x86_64.rpm
-rw-r--r--. 1 root root 86564 Feb 24 15:02 libevent-devel-2.0.21-4.el7.x86_64.rpm
drwxr-xr-x. 3 root root 17 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-httpd.service-qGPJuL
drwxr-xr-x. 3 root root 17 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-vgauthd.service-8p0jur
drwxr-xr-x. 3 root root 17 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-vmtoolsd.service-ouRK2O
drwxr-xr-x. 2 root root 6 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-vmtoolsd.service-ouRK2Ovmware-root
-rw-r--r--. 1 root root 493 Feb 24 15:02 yum_save_tx.2019-02-22.14-22.RXRzka.yumtx
-rw-r--r--. 1 root root 22692 Feb 24 15:02 zabbix_agentd.log
[root@master ~]# salt '*' cmd.run 'ls -l /opt/abc'
192.168.69.202:
total 0
drwx------. 3 root root 17 Jan 7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vgauthd.service-kI2C1K
drwx------. 3 root root 17 Jan 7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vmtoolsd.service-ZzRiqC
//拷贝目录
[root@master ~]# salt '*' file.copy /opt/abc/ /root/abc/ recurse=True remove_existing=True
192.168.69.202:
True
[root@master ~]# salt '*' cmd.run 'ls -l /root/abc'
192.168.69.202:
total 0
drwx------. 3 root root 17 Jan 7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vgauthd.service-kI2C1K
drwx------. 3 root root 17 Jan 7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vmtoolsd.service-ZzRiqC
2.6.9 file.ditectory_exists
判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
192.168.69.202:
total 40
-rw-r--r--. 1 root root 1664 Aug 30 2017 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Aug 30 2017 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Aug 30 2017 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 630 Aug 30 2017 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Aug 30 2017 CentOS-Sources.repo
-rw-r--r--. 1 root root 3830 Aug 30 2017 CentOS-Vault.repo
-rw-r--r--. 1 root root 314 Aug 30 2017 CentOS-fasttrack.repo
drwxr-xr-x. 4 root root 165 Feb 19 09:20 abc
drwxr-xr-x. 81 root root 8192 Feb 22 11:21 etc
drwxr-xr-x. 4 root root 165 Feb 19 09:20 test
[root@master ~]# salt '*' file.directory_exists /opt/abc
192.168.69.202:
True
2.6.10 file.diskusage
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt '*' cmd.run 'du -sb /opt'
192.168.69.202:
27166275 /opt
[root@master ~]# salt '*' file.diskusage /opt
192.168.69.202:
27060683