salt yuyan* pkg.upgrade pkgs=['httpd','tree']
service模块
这个模块相对比较简单,主要是对服务进行管理,官方文档如下:https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.service.html
windows服务管理的如下:
https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.win_service.html
#查看指定服务是否存在
salt yuyan* service.available httpd
#列出所有服务
salt yuyan* service.get_all
#reload指定服务
salt yuyan* service.reload nginx
#restart指定服务
salt yuyan* service.reload nginx
#停止指定服务
salt yuyan* service.stop nginx
#启动指定服务
salt yuyan* service.start nginx
#查看指定服务的状态
salt yuyan* service.status httpd
cron模块
这个也相对简单,主要是对计划任务进行管理,官方文档:https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.cron.html
#列出指定主机,指定用户的所有计划任务
salt yuyan* cron.list_tab root
#返回结果相当于直接执行crontab -l
salt yuyan* cron.raw_cron root
#在指定主机指定用户设置一个计划任务
salt yuyan* cron.set_job root 1 "*" 5 "*" "*" date
#删除指定计划任务,如果不知道cmd,使用cron.list_tab或者cron.ls查看
#关于删除有一些计划任务删除会失败,原因不明
salt yuyan* cron.rm_job root cmd=date
file模块
这个模块相对方法很多,比较复杂,主要是对文件和目录进行管理,这里只列出一些常用的方法示例,大家可以参考官方文档:
https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.file.html
#测试指定文件是否有指定权限,如rwx
salt yuyan* file.access /tmp/test.sh x
#向指定文件追加行,多行使用args
salt yuyan* file.append /tmp/test.sh "date"
salt yuyan* file.append /tmp/test.sh args=["line1","lineN"]
#检查指定文件的hash值
salt yuyan* file.check_hash /tmp/test.sh
更改文件的属组和属主
salt yuyan* file.chown /tmp/test.sh nginx nginx
#注释文件的指定行,可用正则表达式匹配
salt yuyan* file.comment_line /tmp/test.sh '^line'
#复制服务端上面的文件到客户端
salt yuyan* file.copy /etc/passwd /tmp/passwd
#查看客户端指定目录是否存在
salt yuyan* file.directory_exists /tmp/init
#查看指定文件是否存在
salt yuyan* file.file_exists /tmp/test.sh
#查看指定文件占用磁盘的大小
salt yuyan* file.diskusage /tmp/test.sh
#查看客户端/tmp目录下指定文件,根据文件名
salt yuyan* file.find /tmp name=test.sh
#指定类型和文件名
salt yuyan* file.find /tmp name=test.sh type=f
#以正则匹配文件名
salt yuyan* file.find /tmp regex="^t.*sh$"
#找到文件执行删除
salt yuyan* file.find /tmp regex="^t.*sh$" delete
#注:此命令功能强大,更多用法还需要参考官方文档
#查看文件的gid和uid
salt yuyan* file.get_gid /tmp/test.sh.bak
salt yuyan* file.get_uid /tmp/test.sh.bak
#获取文件的权限
salt yuyan* file.get_mode /tmp/test.sh.bak
#更改文件的名字
salt yuyan* file.rename /tmp/test.sh.bak /tmp/test.sh
#删除目录,但目录必须为空
salt yuyan* file.rmdir /tmp/init
#将指定文件里面的line替换为1234
salt yuyan* file.sed /tmp/test.sh "line" "1234"
#创建软件链接
salt yuyan* file.symlink /tmp/test.sh /tmp/test.123
#创建一个文件
salt yuyan* file.touch /tmp/123.txt
user模块
主要用于用户管理,如创建用户,删除用户,更改用户信息等,官方文档:https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.useradd.html
#创建一个test用户,其它都和useradd一样默认
salt yuyan* user.add test
#创建用户时指定shell
salt yuyan* user.add test1 shell=/sbin/nologin
#创建用户时指定不创建家目录
salt yuyan* user.add test3 createhome=False
#创建用户时指定附加组
salt yuyan* user.add test4 groups=nginx
#将test用户加入到nginx组,此为附加组
salt yuyan* user.chgroups test nginx
#查看test用户所有的组
salt yuyan* user.list_groups test
#删除test用户
salt yuyan* user.delete test remove=True
#查看所有用户
salt yuyan* user.list_users
archive模块
主要用于打包,压缩和归档使用,官方文档:https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.archive.html
#打包指定文件,多个文件使用空隔分开,打包后的名称为test.zip
salt yuyan* archive.cmd_zip /opt/test.zip /tmp/test.sh
#打包目录
salt yuyan* archive.cmd_zip /opt/test.zip /tmp/init
#将init.zip解压至/root目录下面
salt yuyan* archive.cmd_unzip /opt/init.zip /root/
#将test.sh打包为test.sh.gz,打包后就在当前目录
salt yuyan* archive.gzip /tmp/test.sh
#将test.sh.gz解压,解压后就在当前目录
salt yuyan* archive.gunzip /tmp/test.sh.gz
#对/tmp/init目录打包后压缩为tar.gz
salt yuyan* archive.tar czvf /opt/init.tar.gz /tmp/init
salt yuyan* iptables.get_saved_rules
network模块
主要用来处理与IP网络有关的事务,官方文档:https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.network.html
#获取主机名
salt yuyan* network.get_hostname
#查看所有网络接口信息
salt yuyan* network.interfaces
#测试与www.baidu.com的连通性
salt yuyan* network.ping
#查看所有活动的tcp连接
salt yuyan* network.active_tcp
查看arp解释表,可以把连接的IP地址解释成MAC地址
salt zabbix* network.arp
#测试指定主机,指定端口的连通性
salt yuyan* network.connect zabbix.aek.com 10051
#查看默认路由
salt yuyan* network.default_route
#获取指定的路由信息
salt **** network.get_route 10.8.3.0
#获取指定网卡的MAC地址
salt yuyan* network.hw_addr eth0
#获取指定网卡的IP地址
salt yuyan* network.interface_ip eth0
#获取指定网卡的IP地址详细信息
salt yuyan* network.interface eth0
扩展模块高级模块:
grains模块
grains模块为minion服务启动的时候从客户端收集的一些硬件,系统基础信息,比如硬件的配置,生产厂商,快速服务代码,系统主机名,IP,环境变量等信息。这些信息我们都可以通过master向minion发送请求获取,官方文档如下:
https://docs.saltstack.com/en/latest/topics/grains/
#获取指定主机的所有grains信息
salt yuyan* grains.items
#获取单个信息,如os,还有其它很多,我们可以通过grains.items查看到所有的
salt yuyan* grains.item os
除了一些已有的信息,我们还可以自定义grains信息,比如我们想给每台主机定义一个角色,方便我们来对服务器分组,对特定的角色执行相关的操作:
修改minion端配置文件,在文件最后新增以下三行
grains:
roles: