saltstack常用功能


1.查看版本

# salt --version

salt 2015.5.8 (Lithium)
[root@localhost salt]# salt --version-report
Usage: salt [options] '<target>' <function> [arguments]
salt: error: no such option: --version-report

# salt --versions-report

      Salt: 2015.5.8

Python: 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
Jinja2: unknown
M2Crypto: 0.20.2
msgpack-python: 0.4.6
msgpack-pure: Not Installed
pycrypto: 2.0.1
libnacl: Not Installed
PyYAML: 3.10
ioflo: Not Installed
PyZMQ: 14.3.1
RAET: Not Installed
ZMQ: 3.2.5
Mako: 0.3.4
Tornado: Not Installed
timelib: Not Installed
dateutil: Not Installed

2. salt-key

查看所的有minion

# salt-key –L

Accepted Keys:
192.168.10.110
192.168.43.218
Denied Keys:
Unaccepted Keys:
192.168.10.110
Rejected Keys:

接受指定的minion

# salt-key –a id
# salt-key -a 192.168.10.110

The following keys are going to be accepted:
Unaccepted Keys:
192.168.10.110
Proceed? [n/Y] Y

接受所有的minion

# salt-key -A

删除指定的minion

# salt-key –d id
# salt-key -d 192.168.10.110

The following keys are going to be deleted:
Accepted Keys:
192.168.10.110
Proceed? [N/y] y
Key for minion 192.168.10.110 deleted.

删除所有的minion

# salt-key -D

The following keys are going to be deleted:
Accepted Keys:
192.168.10.110
192.168.43.218
Proceed? [N/y] y
Key for minion 192.168.10.110 deleted.
Key for minion 192.168.43.218 deleted.

拒绝指定的minion

# salt-key –r id

拒绝所有的minion

# salt-key -R

The following keys are going to be rejected:
Unaccepted Keys:
192.168.10.110
Proceed? [n/Y] Y
Key for minion 192.168.10.110 rejected.

3. minion状态管理

查看存活的minion

# salt-run manage.up

  • 192.168.10.249

查看死掉的minion

#salt-run manage.down

查看down掉的minion,并将其删除

# salt-run manage.down removekeys=True

查看minion的相关状态

# salt-run manage.status
# salt-run manage.status

down:
up:

  • 192.168.10.249

查看slat的所有master和minion的版本信息

# salt-run manage.versions
# salt-run manage.versions

Master:
2015.5.8
2016.Up to date:
----------
192.168.10.249:
2015.5.8

4. 将minion分组

分组规则:

G --针对Grains做单个匹配,例如:G@os:Ubuntu
E --针对minion针对正则表达式匹婚配,例如:E@webd+.(dev|qa|prod).loc
P --针对Grains做正则表达式匹配,例如:P@os:(RedHat|Fedora|CentOS)
L --针对minion做列表匹配,例如:L@minion1.test.com,minion3.domain.com or bl*.domain.com
I --针对 Pillar 做单个匹配,例如:I@pdata:foobar
S --针对子网或是IP做匹配,例如:S@192.168.1.0/24 or S@192.168.1.100
R --针对客户端范围做匹配,例如:R@%foo.bar

编辑master配置文件,注意组名前有两个空格,后有一个空格
# vi /etc/salt/master

nodegroups:
web: 'L@192.168.10.110,192.168.10.111'

# salt -N ‘web’ test.ping

192.168.10.110:
True

首先要保证组员在master的列表中,否则对分组进行操作时会不成功:
# salt -N group1 test.ping

No minions matched the target. No command was sent, no jid was assigned.
ERROR: No return received

5. salt文件管理

编辑master主配置文件,做如下配置:

file_roots:
base:
- /srv/salt
dev:
- /srv/salt/dev
prod:
- /srv/salt/prod

可以将文件及shell放在/srv/salt等文件夹下,执行其他命令调用方法:salt://dev/install.sh

如下执行脚本:
# salt '192.168.10.110' cmd.script salt://test.sh

6. 压缩和解压缩:archive.tar

通过tar压缩或解压文件。

options:Options to pass to the tar command
tarfile:The filename of the tar archive to pack/unpack
sources:Comma delimited list of files to pack into the tarfile. Can also be passed as a Python list.
dest:The destination directory into which to unpack the tarfile
cwd : None,The directory in which the tar command should be executed. If not specified, will default to the home directory of the user under which the salt minion process is running.
template : None
Can be set to 'jinja' or another supported template engine to render the command arguments before execution:
salt '' archive.tar cjvf /tmp/salt.tar.bz2 {{grains.saltpath}} template=jinja
CLI Examples:
# Create a tarfile
salt '
' archive.tar cjvf /tmp/tarfile.tar.bz2 /tmp/file_1,/tmp/file_2
# Unpack a tarfile
salt '*' archive.tar xf foo.tar dest=/target/directory

  • 执行压缩命令:
    # salt '*' archive.tar zcvf /root/test.tar.gz /root/python,/root/testa

    192.168.10.249:
    - tar: Removing leading `/' from member names
    - /root/python/
    - /root/python/p2
    - /root/python/p1
    - /root/testa

通过该方法压缩的文件,解压后带有全路径,可通过cwd指定执行的目录.
# salt '*' archive.tar zcvf /root/test/test.tar.gz python,testa cwd=/root

192.168.10.249:

  • python/
  • python/p2
  • python/p1
  • testa
  • 执行解压缩命令:
    # salt '*' archive.tar zxvf /root/test/test.tar.gz dest=/root/test

    192.168.10.249:
    - python/
    - python/p2
    - python/p1
    - testa

7. 检测:test.ping

测试主机的存活状态
# salt ‘*’ test.ping
# salt '192.168.10.110' test.ping

192.168.10.110:
True

8. 执行shell:cmd.run

在minion上执行shell命令。
# salt '192.168.10.110' cmd.run 'whoami'

192.168.10.110:
root

9. 执行脚本文件:cmd. script

在文件管理中放入相应的脚本文件
# cat /srv/salt/test.sh

#!/bin/bash
echo 'This is a test.'

在minion上执行该脚本:
# salt '192.168.10.110' cmd.script salt://test.sh

192.168.10.110:
----------
pid:
23866
retcode:
0
stderr:
stdout:
This is a test.

10. 查看磁盘利用率:disk.usage

Return usage information for volumes mounted on this minion
CLI Example:
salt '*' disk.usage

11. 更改文件的所属权

更改文件的所属权.
file.chown(path, user, group)
Chown a file, pass the file the desired user and group
path:path to the file or directory
user:user owner
group:group owner
CLI Example:
salt '*' file.chown /etc/passwd root root

12. 复制文件

file.copy(src, dst, recurse=False, remove_existing=False)
将一个目录或文件夹复制到另一个地方
如果复制一个文件夹必须加上recurse=True参数。默认为复制目的文件夹。
remove_existing=True,首先将目的文件夹里的内容全部删除,再进行复制。该参数可以保证源和目的完全一样。
CLI Example:

salt '*' file.copy /path/to/src /path/to/dst
salt '*' file.copy /path/to/src_dir /path/to/dst_dir recurse=True
salt '*' file.copy /path/to/src_dir /path/to/dst_dir recurse=True remove_existing=True

13. 判断文件是否存在

判断某个文件是否存在,返回true/false。
CLI Example:

salt '*' file.file_exists /etc/passwd

14. 判断文件夹是否存在

判断一个文件夹是否存在,返回true/false。
# salt '*' file.directory_exists /root/a

192.168.10.249:
True
# salt '*' file.directory_exists /root/ab
192.168.10.249:
False

15. 查看目录大小

返回目录大小(字节)。
CLI Example:
# salt '*' file.diskusage /root

192.168.10.249:
252580128

16. 查找文件

salt '*' file.find / type=f name=*.bak size=+10m
salt '*' file.find /var mtime=+30d size=+10m print=path,size,mtime
salt '*' file.find /var/log name=*.[0-9] mtime=+30d size=+10m delete

  • 1.查看大小大于10k,后缀为.bak的文件
    # salt '*' file.find /root type=f name=*.bak size=+10k

    192.168.10.249:
    - /root/sabawar/assets/tinymce/jscripts/tiny_mce/plugins/media/js/media.js.bak

    1. 查看大小大于10k,后缀为.bak的文件,返回path,size,mtime
      # salt '*' file.find /root type=f name=*.bak size=+10k print=path,size,mtime

      192.168.10.249:
      - /root/sabawar/assets/tinymce/jscripts/tiny_mce/plugins/media/js/media.js.bak
      - 16485
      - 1453970790

    1. 查看大小大于10k,后缀为.bak的文件,并删除
      # salt '*' file.find /root type=f name=*.bak size=+10k delete

      192.168.10.249:
      - /root/sabawar/assets/tinymce/jscripts/tiny_mce/plugins/media/js/media.js.bak
      # salt '' file.find /root type=f name=\.bak size=+10k
      192.168.10.249:

17. 过滤文件内容

过滤某个文件的内容。同linux的grep。
file.grep(path, pattern, *args)
Note:This function's return value is slated for refinement in future versions of Salt
path:A file path
pattern:A string. For example: test a[0-5]
args:grep options. For example: " -v" " -i -B2"
CLI Example:

salt '*' file.grep /etc/passwd nobody
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr " -i"
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr " -i -B2"
salt '*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr " -i -l"

18. 替换文件内容

# cat a

a
b
c

# salt '*' file.replace /root/test/a pattern='a' repl='aa'

192.168.10.249:
---
+++
@@ -1,3 +1,3 @@
-a
+aa
b
c

# cat a

aa
b
c

19. 更改文件的权限

设置文件(夹)的权限
salt '*' file.set_mode /etc/passwd 0644

20. 添加hosts文件

添加一个host对,如果存在将对应的值添加进去,不存在则创建。
# salt '*' hosts.add_host 192.168.10.249 test.com

192.168.10.249:
True

21. 检测端口

在minion检测某个域名或主机的端口是否打开.
CLI Example:

salt '*' network.connect archlinux.org 80
salt '*' network.connect archlinux.org 80 timeout=3
salt '*' network.connect archlinux.org 80 timeout=3 family=ipv4
salt '*' network.connect google-public-dns-a.google.com port=53 proto=udp timeout=3

# salt '*' network.connect 192.168.10.249 22

192.168.10.249:
----------
comment:
Successfully connected to 192.168.10.249 (192.168.10.249) on tcp port 22
result:
True

22. 获取hostname

Get hostname
CLI Example:

# salt '*' network.get_hostname

192.168.10.249:
localhost.localdomain

23. 同步文件

CLI Example:
salt '' rsync.rsync {src} {dst} {delete=True} {update=True} {passwordfile=/etc/pass.crt} {exclude=xx}
salt '
' rsync.rsync {src} {dst} {delete=True} {excludefrom=/xx.ini}

# salt '*' rsync.rsync /root/test /test

192.168.10.249:
----------
pid:
5572
retcode:
0
stderr:
stdout:
sending incremental file list
test/
test/a
test/a.bak
test/telnet.rpm
test/soft/
test/soft/telnet.rpm
sent 108711 bytes received 96 bytes 72538.00 bytes/sec
total size is 118101 speedup is 1.09

转载于:https://blog.51cto.com/showing/2090354

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值