文章目录
- SaltStack常用模块之salt-cp
- SaltStack常用模块之file
-
- file.access
- file.append
- file.basename
- file.dirname
- file.check_hash
- file.get_hash
- file.get_sum
- file.chattr
- file.chown
- file.copy
- file.directory_exists
- file.file_exists
- file.find
- file.get_gid
- file.get_uid
- file.get_group
- file.get_user
- file.get_mode
- file.get_selinux_context
- file.gid_to_group
- file.group_to_gid
- file.grep
- file.is_blkdev
- file.lsattr
- file.mkdir
- file.move
- file.prepend
- file.sed
- file.read
- file.readdir
- file.remove
- file.remove
- file.set_mode
- file.touch
- file.symlink
- file.uid_to_user
- file.user_to_uid
- file.write
SaltStack常用模块之salt-cp
salt-cp能够很方便的把 master 上的文件批量传到 minion上
//拷贝单个文件到目标主机的/usr/src目录下
[root@master ~]# salt '*' cmd.run 'ls /usr/src'
node1:
debug
kernels
node2:
debug
kernels
[root@master ~]# salt-cp '*' /etc/passwd /usr/src/
node1:
----------
/usr/src/passwd:
True
node2:
----------
/usr/src/passwd:
True
[root@master ~]# salt '*' cmd.run 'ls /usr/src'
node1:
debug
kernels
passwd
node2:
debug
kernels
passwd
//拷贝多个文件到目标主机的/usr/src目录下
[root@master ~]# salt-cp '*' /etc/passwd /etc/group /usr/src/ //最后一个路径为目标主机的路径
node1:
----------
/usr/src/group:
True
/usr/src/passwd:
True
node2:
----------
/usr/src/group:
True
/usr/src/passwd:
True
[root@master ~]# salt '*' cmd.run 'ls /usr/src'
node1:
debug
group
kernels
passwd
node2:
debug
group
kernels
passwd
SaltStack常用模块之file
file.access
检查指定路径是否存在
[root@master ~]# salt '*' cmd.run 'ls /usr/src'
node1:
debug
group
kernels
passwd
node2:
debug
group
kernels
passwd
[root@master ~]# salt '*' file.access /usr/src/passwd f //f检查文件是否存在
node1:
True
node2:
True
[root@master ~]# salt '*' file.access /usr/src/123 f
node1:
False
node2:
False
检查指定文件的权限信息
[root@master ~]# salt '*' cmd.run 'ls -l /usr/src'
node1:
total 8
drwxr-xr-x. 2 root root 6 May 19 2020 debug
-rw-r--r-- 1 root root 499 Nov 4 18:09 group
drwxr-xr-x. 2 root root 6 May 19 2020 kernels
-rw-r--r-- 1 root root 1070 Nov 4 18:09 passwd
node2:
total 8
drwxr-xr-x. 2 root root 6 May 11 2019 debug
-rw-r--r-- 1 root root 499 Nov 4 18:09 group
drwxr-xr-x. 2 root root 6 May 11 2019 kernels
-rw-r--r-- 1 root root 1070 Nov 4 18:09 passwd
//检查权限信息时需要注意的是用户、组、其他三部分只要是其中有一部分满足即返回值为true
[root@master ~]# salt '*' file.access /usr/src/passwd r
node1:
True
node2:
True
[root@master ~]# salt '*' file.access /usr/src/passwd x
node1:
False
node2:
False
[root@master ~]# salt '*' file.access /usr/src/passwd w
node1:
True
node2:
True
file.append
往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt '*' cmd.run 'ls -l /root'
node1:
total 4
-rw-------. 1 root root 1082 Jul 27 18:44 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Nov 4 18:19 test
node2:
total 4
-rw-------. 1 root root 1169 Jul 10 20:37 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Nov 4 18:18 test
//追加内容,追加的内容之间有空格需要加引号,每一行的内容也需要加引号
[root@master ~]# salt '*' file.append /root/test "hello world" "hello saltstack"
node1:
Wrote 2 lines to "/root/test"
node2:
Wrote 2 lines to "/root/test"
[root@master ~]# salt '*' cmd.run 'cat /root/test'
node1:
hello world
hello saltstack
node2:
hello world
hello saltstack
//追加的文件不存在,报错
[root@master ~]# salt '*' file.append /root/haha "hello world" "hello saltstack"
FileNotFoundError: [Errno 2] No such file or directory: '/root/haha
file.basename
获取指定路径的基名(指定的文件或者目录存不存在都可以)
[root@master ~]# salt '*' file.basename '/usr/local/httpd'
node1:
httpd
node2:
httpd
file.dirname
获取指定路径的目录名(指定的目录或者文件存不存在都可以)
[root@master ~]# salt '*' file.dirname '/usr/local/httpd'
node1:
/usr/local
node2:
/usr/local
file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
//查看指定文件的hash值
[root@master ~]# salt '*' cmd.run 'md5sum /root/anaconda-ks.cfg'
node1:
05cc46813b0af57b91b4f34a016192a7 /root/anaconda-ks.cfg
node2:
b497e404d9918e40014c29cab5c92b32 /root/anaconda-ks.cfg
//核查hash值是否一致
[root@master ~]# salt '*' file.check_hash '/root/anaconda-ks.cfg' b497e404d9918e40014c29cab5c92b32
node1:
False
node2:
True //指定的是node2上hash值,返回true
file.get_hash
获取指定文件的hash值,该值通过 sha256 算法得来
//查看hash值
[root@master ~]# salt '*' cmd.run 'sha256sum /root/test'
node2:
30a21149cca7bab54bb8417a46862901c08d480ddb5d31f607e6abf3466f22a5 /root/test
node1:
30a21149cca7bab54bb8417a46862901c08d480ddb5d31f607e6abf3466f22a5 /root/test
//获取hash值
[root@master ~]# salt '*' file.get_hash /root/test
node1:
30a21149cca7bab54bb8417a46862901c08d480ddb5d31f607e6abf3466f22a5
node2:
30a21149cca7bab54bb8417a46862901c08d480ddb5d31f607e6abf3466f22a5
file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
//使用默认hash值
[root@master ~]# salt '*' cmd.run 'sha256sum /root/test'
node2:
30a21149cca7bab54bb8417a46862901c08d480ddb5d31f607e6abf3466f22a5 /root/test
node1:
30a21149cca7bab54bb8417a46862901c08d480ddb5d31f607e6abf3466f22a5 /root/test
[root@master ~]# salt '*' file.get_sum /root/test
node1:
30a21149cca7bab54bb8417a46862901c08d480ddb5d31f607e6abf3466f22a5
node2:
30a21149cca7bab54bb8417a46862901c08d480ddb5d31f607e6abf3466f22a5
//使用MD5
[root@master ~]# salt '*' cmd.run 'md5sum /root/test'
node2:
7d2d5cb4606a44579f400d8de6b1bf57 /root/test
node1:
7d2d5cb4606a44579f400d8de6b1bf57 /root/test
[root@master ~]# salt '*' file.get_sum /root/test md5
node1:
7d2d5cb4606a44579f400d8de6b1bf57
node2:
7d2d5cb4606a44579f400d8de6b1bf57
file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据, 不允许任何进程覆盖或截断这个文件(不允许删除) |
只允许在这个目录下建立和修改文件 |