Saltstack基础总结
saltstack
镜像源
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.
# sed -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
# yum clean all
# yum repolist all
wget -P /etc/yum.repos.d https://mirrors.aliyun.com/saltstack/yum/redhat/7.2/x86_64/saltstack-rhel7.repo
关于key的认证信息
/etc/salt/master 没有配置auto_accept: True时,需要通过salt-key命令来进行证书认证操作
salt-key -L 显示已认证或未认证的被控端id
salt-key -D 删除所有认证主机id 证书
salt-key -d id删除单个id证书
salt-key -A 接受所有id证书
salt-key -a id接受单个id证书
salt-key -j id 拒绝单个id证书
salt-key –J 拒绝所有id证书
1.minion在第一次启动时,会在/etc/salt/pki/minion/(该路径在/etc/salt/minion里面设置)下自动生成minion.pem(private key)和minion.pub(publickey),然后将minion.pub发送给master。2.master在接收到minion的public key后,通过salt-key命令accept minion public key,这样在master的/etc/salt/pki/master/minions下将会存放以minion id命名的public key,然后master 就能对minion发送指令了。


日志查看路径:
服务端:/var/log/salt/master
客户端:/var/log/salt/minion
常用的命令集合:
runner 模块
master和minion的状态命令:
salt-run manage.status 查看所有的minion状态
salt-run manage.up 查看所有的在线的minion状态
salt-run manage.down 查看所有离线的minion状态
salt -E, --pcre 通过正则表达式进行匹配 例如: salt -E ‘^server*’ test.ping 记得前面加^
salt -L, --list 以主机id名形式来过滤信息的 例如: salt -L '192.168.1.1,192.168.1.2' test.ping
salt -G,--grain 根据被控机的grains信息进行过滤 salt '*' grains.items 查看所有的grains的信息
例如:salt -G 'cpuarch:x86_64' cmd.run 'uptime' 在所有64位CPU服务器上执行uptime
grains的作用是收集被控主机的基本信息,这些信息通常都是一些静态的数据,包括CPU、内核、操作系统、虚拟化等。
salt -I,--pillar根据被控主机的pillar信息进行过滤
-N,--nodegroup 根据主控端master配置文件中的分组名称进行过滤

将nodegroup.conf的文件,放到该目录存放的位置为 /etc/salt/master.d
该文件的书写要求如下 :

Module模块
salt '*' sys.list_modules 查看所有的模块的信息
通过master同步到minion端,在minion端执行的
salt-call saltutil.sync_modules salt-call saltutil.sync_all
包括,beacons,clouds,engines,grains,log_handlers,modules,output,proxymodules,renderers,returners,sdb,states,utils
archive模块
salt '*' archive.gzip /tmp/1234.zip /tmp/sourcefile.txt
salt '*' archive.unzip /opt/a.zip /tmp/
#支持gunzip、gzip、rar、tar、unrar、unzip等。
Grins 模块
记录minion的属性 key:value 自定义grians(在minion上定义的)
- salt "*" grains.ls 列出所有grains项目名字

- salt "*app.*" grains.items 列出所有grains项目以及值
grains的信息并不是动态的,并不会实时变化,它只是在minion启动时收集到的,我们可以根据grains收集到的一些信息,做一些配置管理工作。
在minion上:vim /etc/salt/grains
role: nginx
env: test
重启:service salt-minion restart
salt "*" grains.item role env 获取grians
或者:
salt -G "*" role:nginx cmd.run "hostname"
salt ‘*’grains.items

Pillar模块
记录所有minion通用的属性,然后同步到minion端
salt-call saltutil.refresh_pillar
salt ‘*’ saltutil.refresh_pillar
pillar(在master上定义)(yaml语法)
在配置文件中找pillar的文件路劲,找到以后做如下操作:
mkdir /home/salt/pillar
vim top.sls
base:
"*":
- test
vim test.sls
conf: wang
然后刷新pillar:
salt '*' saltutil.refresh_pillar
验证:salt '*' pillar.items conf
或者:salt -I 'conf:xiang' test.ping
cmd模块
salt ‘*’ cmd.run “df -h”
ping模块
salt ‘*’ test.ping –t 5
cp 模块
可以在master的配置文件中配置:
file_roots:
base:
- /home/salt/
salt的根目录就是file_roots定义的路径,salt://test.txt相当于/home/salt/test.txt
salt 192.168.1.1 cp.get_file salt://app.conf /tmp/xiang.txt
cron模块
salt '*' cron.raw_cron root (查看定时任务)
salt '*' cron.set_job root '*' '*' '*' '*' 1 /export/scripts/rm_log.sh
salt '*' cron.rm_job root /export/scripts/rm_log.sh (写全没效果)
dnsutil模块
salt '*' dnsutil.hosts_append /etc/hosts 127.0.0.1 xiang.com
salt '*' dnsutil.hosts_remove /etc/hosts xiang.com
file模块
salt '*' file.chown /etc/passwd root root 更改minion端的权限
salt '*' file.copy /path/to/src /path/to/dst 是复制minion端的文件
salt '*' file.file_exists /etc/hosts
salt '*' file.directory_exists /etc/
salt '*' file.get_mod /etc/passwd
salt '*' file.set_mod /etc/passwd 0644
salt '*' file.mkdir /tmp/test
salt '*' file.sed /export/servers/nginx/conf/nginx.conf 'debug' 'warn'
salt '*' file.append /tmp/test.txt "welcome xiang"
salt '*' file.remove /tmp/1.txt
network模块
salt '*' network.dig www.qq.com
salt '*' network.ping www.qq.com
salt '*' network.ip_addrs
pkg包管理模块,管理yum, apt-get等
salt '*' pkg.install php
salt '*' pkg.remove php
salt '*' pkg.upgrade (升级所有的软件包)
service模块
salt '*' service.enable nginx
salt '*' service.disable nginx
salt '*' service.restart nginx
自定义模块
首先在默认目录中,创建一个modules模块的目录:
[root@python salt]# mkdir _modules
进入 _modules 并编写 module文件:
[root@python _modules]# vim hello.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def hello():
result = dict()
result.update({"code": 231})
result.update({"messages": "successful"})
result.update({"Get": "YES"}
return result
3)、刷新模块
[root@python _modules]# salt '*' saltutil.sync_modules
python2:
- modules.hello
4)、引用模块
[root@python _modules]# salt "*" hello.hello
python2:
----------
Get:
YES
code:
231
messages:
successful
或者以json的格式输出:
[root@python _modules]# salt "*" hello.hello --output json
{
"python2": {
"code": 231,
"messages": "successful",
"Get": "YES"
}
}
假如所写的函数 需要传参:
[root@python _modules]# cat hello.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def hello(aaa):
result = dict()
result.update({"code": 231})
result.update({"messages": 'successful'})
result.update({"Get": aaa})
return result
1)、更新
[root@python _modules]# salt '*' saltutil.sync_modules
python2:
- modules.hello
2)、传入参数
[root@python _modules]# salt "*" hello.hello aaa --output json
{
"python2": {
"code": 231,
"messages": "successful",
"Get": "aaa"
}
}