salt安装
第一步 主机名区分好,十分关键
centos7 设置主机名方式
hostnamectl set-hostname centos7
1 在master节点上 192.168.255.128
curl -o /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install salt-master salt-minion
在slave 节点上 192.168.255.129
curl -o /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install salt-minion
2 设置开机自动启动
systemctl enable salt-master
systemctl enable salt-minion
3 先启动 master
systemctl start salt-master
4 修改minion 配置文件
vim /etc/salt/minion
maser机器
第16行: master: 192.168.255.128
id: linux-node1.oldboyedu.com
slave机器
第16行: master: 192.168.255.128
id: linux-node2.oldboyedu.com
注意:如果 id不配,就默认取主机名,所以之前就要求配置好主机名
然后启动 minion
systemctl start salt-minion.service
5 salt-stack 对 minion 进行认证
在 minion 启动的时候,会在
/etc/salt/pki/minion/ 目录下 生成 一个公钥 和 一个私钥
minion.pem minion.pub
minion 会把 公钥 发给master
这是需要 master 执行命令同意
salt-key 查看 所有key
包括以接受的,未接受的,拒绝的
Accepted Keys:
Denied Keys:
Unaccepted Keys:
linux-node1.oldboyedu.com
linux-node2.oldboyedu.com
Rejected Keys:
salt-key -A
接受所有的key
也可以支持通配符 salt-key -a linux*
远程执行详解
执行第一条命令
salt '*' test.ping
* 是代表所有,test是一个模块,ping是这个模块的一个方法
salt '*' cmd.run 'uptime'
uptime run方法的入参
远程执行 命令行选项
与所有标准的linux 命令一样,salt 的命令行选项的作用就是通过不同的选项来改变程序本身的行为
-v or -verbose: 开启命令的详细的描述
salt --verbose "*" cmd.run_all "echo my salt"
Executing job with jid 20180924212652325729
-------------------------------------------
linux-node1.oldboyedu.com:
----------
pid:
8054
retcode:
0
stderr:
stdout:
my salt
linux-node2.oldboyedu.com:
----------
pid:
3495
retcode:
0
stderr:
stdout:
my salt
--summary 显示一条salt命令的概要
salt --summary "*" cmd.run_all "echo my salt"
linux-node1.oldboyedu.com:
----------
pid:
8147
retcode:
0
stderr:
stdout:
my salt
linux-node2.oldboyedu.com:
----------
pid:
3517
retcode:
0
stderr:
stdout:
my salt
-------------------------------------------
Summary
-------------------------------------------
# of Minions Targeted: 2
# of Minions Returned: 2
# of Minions Did Not Return: 0
-------------------------------------------
--out : 控制salt 执行后输出结果的格式
salt --out=json "*" cmd.run_all "echo my salt"
--out选项还可以选择raw,nested,quiet等
{
"linux-node1.oldboyedu.com": {
"pid": 8191,
"retcode": 0,
"stderr": "",
"stdout": "my salt"
}
}
{
"linux-node2.oldboyedu.com": {
"pid": 3532,
"retcode": 0,
"stderr": "",
"stdout": "my salt"
}
}
sys 模块的list_modules 可以列举 对应的 minion 主机上所有模块
salt "linux-node1.oldboyedu.com" sys.list_modules
linux-node1.oldboyedu.com:
- acl
- aliases
- alternatives
- archive
- artifactory
- at
- augeas
。。。
用 sys 模块的 list_functions 可以列举出模块内 可以应用的函数
salt "linux-node1.oldboyedu.com" sys.list_functions test
- test.arg
- test.arg_repr
- test.arg_type
- test.assertion
- test.attr_call
- test.collatz
- test.conf_test
。。。
通过 sys.doc 可以查询到详细用法
salt "linux-node1.oldboyedu.com" sys.doc test.ping
'test.ping:'
Used to make sure the minion is up and responding. Not an ICMP ping.
Returns ``True``.
CLI Example:
salt '*' test.ping
常用的模块和函数
1 远程命令执行模块
salt "*" cmd.run "uptime"
2 显示更详细信息的函数
salt "*" cmd.run_all "uptime"
3 安装包管理
pkg.install 安装程序包
salt "linux-node2.oldboyedu.com" pkg.install "httpd"
查看已安装软件的版本信息
salt "linux-node2.oldboyedu.com" pkg.version "httpd"
删除已经安装的软件包
salt "linux-node2.oldboyedu.com" pkg.remove "httpd"
4 管理服务模块
查看服务状态
salt "linux-node2.oldboyedu.com" service.status httpd
linux-node2.oldboyedu.com:
False
如果 service.status远程执行模块函数输出True结果,则表示该服务正在运行;如果输出结果为False,则代表服务停止
开启服务
salt "linux-node2.oldboyedu.com" service.start httpd
linux-node2.oldboyedu.com:
True
再 执行 salt "linux-node2.oldboyedu.com" service.status httpd
linux-node2.oldboyedu.com:
True
关闭服务
salt "linux-node2.oldboyedu.com" service.stop httpd
linux-node2.oldboyedu.com:
True
文件管理模块
文件信息查询:
salt "linux-node2.oldboyedu.com" file.stats /etc/yum.conf
linux-node2.oldboyedu.com:
----------
atime:
1537786671.22
ctime:
1533996993.54
gid:
0
group:
root
inode:
35497259
mode:
0644
mtime:
1523624328.0
size:
970
target:
/etc/yum.conf
type:
file
uid:
0
user:
root
文件属组修改:
salt "*" file.chown /etc/passwd root root
5 用户管理模块
某些时候,用户管理是系统管理员必须执行的一个任务
添加用户 的 salt 命令格式
salt '*' user.add name <uid> <gid> <groups> <home> <shell>
添加 用户
salt "linux-node2.oldboyedu.com" user.add 'mysql'
删除用户
salt "linux-node2.oldboyedu.com" user.delete 'mysql'
查看用户信息
salt "linux-node2.oldboyedu.com" user.info 'root'
目标定位字符串
在前面的例子中,我们只运行了两个minion,而实际环境中的salt通常都是控制着成百上钱的minion
1 全局匹配
salt "*" test.ping
熟悉linux命令的读者对通配符一定不陌生,都知道*代表任意字符。在全局匹配的方式中,salt 匹配字符串的写法基本和shell方式相同
*:代表任意字符串,也可以是空字符串
例如 salt 'linux*' test.ping
?:代表一个字符,但不可以为空;
例如 salt 'linux-node?.oldboyedu.com' test.ping
[] : 字符集合,例如[a-z]代表任何一个小写字母,[0-9]代表数字
例如 salt 'linux-node[1-9].oldboyedu.com' test.ping
正则表达式匹配
如果我们需要执行更为复杂的匹配, 比如针对某些minion名称的匹配,我们可以使用正则表达式。salt使用Python的re正则表达式,可以解析正则表达式(PCRE),
正则表达式匹配需要配合命令行选项-E 或 -pcre
例如 完全 匹配
salt -E "linux-node1.oldboyedu.com" test.ping
linux-node1.oldboyedu.com:
True
salt -E "linux-node" test.ping
linux-node2.oldboyedu.com:
True
linux-node1.oldboyedu.com:
True
在正则表达式匹配方式下全局匹配可以用如下方法:
salt -E ".*" test.ping
以特殊字符开头或结尾的匹配方式:
salt -E "^linux-.*" test.ping
salt -E ".*com$" test.ping
有时候,我们只想匹配一个指定列表里面的主机并进行远程执行操作,这时可能全局匹配方式和正则表达式方式都无法很好地完成任务,这种情况下我们可以
直接使用-L选项进行列表匹配
salt -L "linux-node2.oldboyedu.com" test.ping
linux-node2.oldboyedu.com:
True
salt -L "linux-node2.oldboyedu.com,linux-node1.oldboyedu.com" test.ping
linux-node1.oldboyedu.com:
True
linux-node2.oldboyedu.com:
True
通常列表匹配的主机都写在 master的匹配文件中,/etc/salt/master中以nodegroups形式出现,包括正则全局匹配等方式都可以写在配置文件中,然后通过
分组匹配选项-N 加上nodegroups名称进行匹配
vim /etc/salt/master
nodegroups:
minions: 'linux-node2.oldboyedu.com'
salt -N minions test.ping
linux-node2.oldboyedu.com:
True
其中 minions 包含了 linux-node2.oldboyedu.com
grain 和 pillar 匹配
Grains 和 Pillars都是以key value 形式存储的数据库,Grains是由minion返回给master的数据,而Pillars数据则是存储在master上的。每个minion只能看到自己的pillar
,Grain可以看作Host的meatdata(元数据),例如CPU的数量;而Pillars则是主机所需要的数据(例如数据库密码)。换言之,一个minion可以告诉master它的Grain
数据,而minion则需要从master索要Pillar数据。
Grains
Grains 可以认为是描述minion本身固有属性的静态数据。例如,minion 服务器的操作系统版本,内存大小,网卡的MAC地址等
例如 salt "linux-node2.oldboyedu.com" grains.items
SSDs:
biosreleasedate:
05/20/2014
biosversion:
6.00
....
cpu_model:
Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz
cpuarch:
x86_64
domain:
oldboyedu.com
fqdn:
linux-node2.oldboyedu.com
检索某一 Grains 数据
salt "linux-node2.oldboyedu.com" grains.item os
我们可以使用Grains 定位主机,利用-G 或 --grain
例如 我们 只对 Centos 的机器进行定位
salt -G "os:CentOS" test.ping
linux-node2.oldboyedu.com:
True
linux-node1.oldboyedu.com:
True
grain 全局匹配
salt -G 'osrelease:7.*' test.ping
linux-node1.oldboyedu.com:
True
linux-node2.oldboyedu.com:
True
Pillars
Pillars 数据同Grains 相似,不同之处是Pillars数据 可以定义为更加动态的形式,并且是一个安全的数据存储库。pillars 也是 key value 形式存储的数据,
也可以和Grain一样进行匹配。不同的是,需要使用-l 或 --pillar 选项
列出 主机所有的 pillar 数据:
salt "linux-node1.oldboyedu.com" pillar.items
linux-node1.oldboyedu.com:
----------
鸣谢:
老男孩教育
saltstack运维实战