第三十八课 自动化运维之Saltstackhtml
目录node
1、自动化运维介绍
2、 saltstack安装
3、 启动saltstack服务
4、 saltstack配置认证
5、 saltstack远程执行命令
6、 grains
7、 pillar
8、 安装配置httpd
9、 配置管理文件
10、 配置管理目录
11、 配置管理远程命令
12、 配置管理计划任务
十3、 其余命令
十4、 salt-ssh使用python
认识自动化运维linux
传统运维效率低,大多工做人为完成ios
传统运维工做繁琐,容易出错nginx
传统运维每日重复作相同的事情web
传统运维没有标准化流程正则表达式
传统运维的脚本繁多,不能方便管理shell
自动化运维就是要解决上面全部问题apache
常见自动化运维工具
Puppet (www.puppetlabs.com)基于ruby开发,c/s架构,支持多平台,可管理配置文件、用户、cron任务、软件包、系统服务等。 分为社区版(免费)和企业版(收费),企业版支持图形化配置。
Saltstack(官网 https://saltstack.com,文档docs.saltstack.com )基于python开发,c/s架构,支持多平台,比puppet轻量,在远程执行命令时很是快捷,配置和使用比puppet容易,能实现puppet几乎全部的功能。
Ansible (www.ansible.com )更加简洁的自动化运维工具,不须要在客户端上安装agent,基于python开发。能够实现批量操做系统配置、批量程序的部署、批量运行命令。
saltstack介绍
https://docs.saltstack.com/en/latest/topics/index.html
saltstack既可使用salt-ssh远程执行,相似ansible,也支持c/s模式。
演示环境:
saltserver 192.168.1.41 CentOS release 6.8 (Final)
saltminion 192.168.1.43 CentOS release 6.8 (Final)
saltminion01 192.168.1.42 CentOS release 6.8 (Final)
三台机器都配置hosts
# vim /etc/hosts // 添加 192.168.1.41 saltserver.local 1921.68.1.42 saltminion01.local 192.168.1.43 saltminion.local
Saltstack安装
1.安装epel-release源
// 也能够从http://repo.saltstack.com/yum/redhat/下载安装salt最新的源 [root@saltserver ~]# yum -y install epel-release
2.安装相应的salt包
// 查看salt安装包 [root@saltserver ~]# yum list | grep salt python-salttesting.noarch 2015.7.10-1.el6 epel salt.noarch 2015.5.10-2.el6 epel salt-api.noarch 2015.5.10-2.el6 epel salt-cloud.noarch 2015.5.10-2.el6 epel salt-master.noarch 2015.5.10-2.el6 epel salt-minion.noarch 2015.5.10-2.el6 epel salt-ssh.noarch 2015.5.10-2.el6 epel salt-syndic.noarch 2015.5.10-2.el6 epel // 服务器端安装salt-master和salt-minion [root@saltserver ~]# yum -y install salt-master.noarch salt-minion.noarch // 客户端安装salt-minion便可 [root@saltminion01 ~]# yum -y install salt-minion.noarch
1.启动salt-master
[root@saltserver ~]# /etc/init.d/salt-master start // 服务端监听4505和4506两个端口,4505为消息发布的端口,4506为和客户端通讯的端口 [root@saltserver ~]# netstat -nltup | egrep "4505|4506" tcp 0 0 0.0.0.0:4505 0.0.0.0:* LISTEN 1589/python2.6 tcp 0 0 0.0.0.0:4506 0.0.0.0:* LISTEN 1609/python2.6
2.编辑minion端的配置文件,并启动minion
// 以saltminion01为例 [root@saltminion01 ~]# vim /etc/salt/minion // 修改master为服务端的主机名或ip地址,若是规模较大也能够搭建本地dns服务来提供名称解析服务 master: saltserver.local // 修改id值为客户端的主机名,这会作为服务端上显示的客户端标识 #id: id: saltminion01.local // 启动minion [root@saltminion01 ~]# /etc/init.d/salt-minion start
master端和minion端通讯须要创建一个安全通道,传输过程须要加密,因此得配置认证,也是经过密钥对来加密解密的
minion在第一次启动时会在/etc/salt/pki/minion/下生成minion.pem和minion.pub,其中.pub为公钥,它会把公钥传输给master
master第一次启动时也会在/etc/salt/pki/master下生成密钥对,当master接收到minion传过来的公钥后,经过salt-key工具接受这个公钥,一旦接受后就会在/etc/salt/pki/master/minions/目录里存放刚刚接受的公钥,同时客户端也会接受master传过去的公钥,把它放在/etc/salt/pki/minion目录下,并命名为minion_master.pub
以上过程须要借助salt-key工具来实现
salt-key经常使用选项
-a 后面跟主机名,认证指定主机 -A 认证全部主机 -r 跟主机名,拒绝指定主机 -R 拒绝全部主机 -d 跟主机名,删除指定主机认证 -D 删除所有主机认证 -y 省略掉交互,至关于直接按了y
显示key
// 服务端 [root@saltserver ~]# salt-key -L Accepted Keys: Denied Keys: Unaccepted Keys: saltminion.local saltminion01.local saltserver Rejected Keys: [root@saltserver ~]#
认证主机
// 能够一台一台认证,也能够批量认证 [root@saltserver ~]# salt-key -a saltserver The following keys are going to be accepted: Unaccepted Keys: saltserver Proceed? [n/Y] Y Key for minion saltserver accepted. [root@saltserver ~]# salt-key -L Accepted Keys: saltserver Denied Keys: Unaccepted Keys: saltminion.local saltminion01.local Rejected Keys: // 批量认证,也可经过修改配置文件实现 // 在 /etc/salt/master中取消下句注释 #auto_accept: True [root@saltserver ~]# salt-key -A The following keys are going to be accepted: Unaccepted Keys: saltminion.local saltminion01.local Proceed? [n/Y] Y Key for minion saltminion.local accepted. Key for minion saltminion01.local accepted. [root@saltserver ~]# salt-key -L Accepted Keys: saltminion.local saltminion01.local saltserver Denied Keys: Unaccepted Keys: Rejected Keys:
删除主机认证
// 以saltminion01为例,在服务器上操做, -D删除全部认证 [root@saltserver ~]# salt-key -d saltminion01.local -y Deleting the following keys: Accepted Keys: saltminion01.local Key for minion saltminion01.local deleted. // minion端重启服务 [root@saltminion01 ~]# /etc/init.d/salt-minion restart Stopping salt-minion daemon: [ OK ] Starting salt-minion daemon: [ OK ] // 服务器再次检查key [root@saltserver ~]# salt-key -L Accepted Keys: saltminion.local saltserver Denied Keys: Unaccepted Keys: saltminion01.local Rejected Keys:
拒绝key
// -R 拒绝全部 [root@saltserver ~]# salt-key -r saltminion01.local -y Key for minion saltminion01.local rejected. [root@saltserver ~]# salt-key -L Accepted Keys: saltminion.local saltserver Denied Keys: Unaccepted Keys: Rejected Keys: saltminion01.local
远程执行命令语法
salt <target> <模块名>.<方法> [参数…]
target支持如下类型
Glob通配,salt的默认类型
// 模块的保存位置/usr/lib/python2.6/site-packages/salt/modules/ // 方法能够查看模块vim /usr/lib/python2.6/site-packages/salt/modules/test.py, // 也能够从salt的官方文档查询 // 查看minion可用模块salt 'target' sys.list_modules // 查看模块的函数 salt 'target' sys.list_functions test // 系统帮助文件 salt 'target' sys.doc test.ping // *表示全部主机,test表明模块,ping是方法。 [root@saltserver ~]# salt "*" test.ping saltminion01.local: True saltminion.local: True saltserver: True
-L,长选项--list,该选项通个逗号分隔的列表来指定多个Minion。列表中不使用glob或正则表达式进行模式
[root@saltserver ~]# salt -L saltminion.local,saltminion01.local test.ping saltminion.local: True saltminion01.local: True
短选项:-S,长选项: --ipcidr。经过过指定一个IPv4地址或一个CIDR的ipv4子网来target minion。
[root@saltserver ~]# salt -S 192.168.1.0/24 test.ping saltminion01.local: True saltminion.local: True saltserver: True
短选项: -E 长选项: --pcre。Perl语言兼容正则表达式(PCRE)
[root@saltserver ~]# salt -E '^saltserver$' test.ping saltserver: True [root@saltserver ~]# salt -E '.*.local$' test.ping saltminion.local: True saltminion01.local: True
短选项:-G 长选项: --grain。
// 对os是CentOS的主机进行ping测试 [root@saltserver ~]# salt -G "os:CentOS" test.ping saltserver: True saltminion01.local: True saltminion.local: True
Grain PCRE 短选项: 无 长选项:--grain-pcre
salt –grain-pcre ‘os:red(hat|flag) test.ping’
Pillar 短选项: -I,长选项: --pillar
实例见下,pillar部分
混合(compound),短选项: -C,长选项: --compound。混合target容许用户在一个shell命令中指定多种target类型。默认使用glob,想指定其余target类型,则须要在前面追加上类型简写和@符号
简写 | target |
---|---|
G | Grain |
E | PCRE Minion ID |
P | Grain PCRE |
L | 列表 |
I | Pillar |
S | 子网/IP地址 |
R | SECO范围 |
# salt -C ‘G@os:Ubuntu,I@role:web,S@192.168l100.0/24’ test.ping
节点组(nodegroup)短选项:-N,长选项:--nodegroup。在命令行使用前必须先在master的配置文件中以target列表进行定义(使用混合匹配语法)
nodegroups: webdev: 'I@role:web,G@cluster:dev' webdqa: 'I@role:web,G@cluster:qa' webprod: 'I@role:web,G@cluster:prod'
节点组定义完毕并重载Master配置文件后,能够经过salt进行target:
salt -N wendev test.ping
grains是在minion启动时收集到的一些信息,好比操做系统类型、网卡ip、内核版本、cpu架构等。
列出全部的grains项目名字
[root@saltserver ~]# salt 'saltminion01.local' grains.ls | head -n 10 saltminion01.local: - SSDs - biosreleasedate - biosversion - cpu_flags - cpu_model - cpuarch - domain - fqdn - fqdn_ip4
列出全部grains项目以及值
[root@saltserver ~]# salt 'saltminion01.local' grains.items | head -n 10 saltminion01.local: ---------- SSDs: biosreleasedate: 07/02/2015 biosversion: 6.00 cpu_flags: - fpu - vme
grains的信息并非动态的,并不会实时变动,它是在minion启动时收集到的。
咱们能够根据grains收集到的一些信息,作配置管理工做。
[root@saltserver ~]# salt -G 'os:CentOS' cmd.run 'hostname' saltminion.local: saltminion.local saltminion01.local: saltminion01.local saltserver: saltserver.local
grains支持自定义信息。
// 在客户端编辑/etc/salt/grains文件 [root@saltminion01 ~]# vim /etc/salt/grains // 添加以下两行自定义信息 role: nginx env: test // 重启minion服务 [root@saltminion01 ~]# /etc/init.d/salt-minion restart Stopping salt-minion daemon: [ OK ] Starting salt-minion daemon: [ OK ] // 在服务端查看自定义的信息 [root@saltserver ~]# salt 'saltminion01.local' grains.item role env saltminion01.local: ---------- env: test role: nginx // 能够利用自定义的信息来做为target使用 [root@saltserver ~]# salt -G 'role:nginx' cmd.run 'hostname' saltminion01.local: saltminion01.local
pillar和grains不同,是在master上定义的,而且是针对minion定义的一些信息。像一些比较重要的数据(密码)能够存在pillar里,还能够定义变量等。
配置自定义pillar
// 在服务器端操做 [root@saltserver ~]# vim /etc/salt/master // 取消下三行的注释 // salt中只能用空格对齐,不能用tab代替空格。空格数不限制,可是相同级别的缩进的空格数一致。 pillar_roots: base: - /srv/pillar // 新建/src/pillar rm: remove directory `/src/pillar'? y [root@saltserver ~]# mkdir /srv/pillar [root@saltserver ~]# ls -l !$ ls -l /srv/pillar total 0 // 在/srv/pillar新建test.sls [root@saltserver pillar]# echo 'conf: /etc/123.conf'>> test.sls [root@saltserver pillar]# cat test.sls conf: /etc/123.conf // 在/srv/pillar下新建top.sls conf: /etc/123.conf [root@saltserver pillar]# cat >>top.sls<<EOF > base: > 'saltminion01': > - test > EOF [root@saltserver pillar]# cat top.sls base: 'saltminion01': - test // 重启master [root@saltserver pillar]# /etc/init.d/salt-master restart Stopping salt-master daemon: [ OK ] Starting salt-master daemon: [ OK ] // 刷新pillar配置来获取新的pillar状态 [root@saltserver pillar]# salt '*' saltutil.refresh_pillar saltminion01.local: True saltminion.local: True saltserver: True // 验证 [root@saltserver pillar]# salt '*' pillar.item conf saltserver: ---------- saltminion.local: ---------- saltminion01.local: ---------- conf: /etc/123.conf //pillar一样能够用来做为salt的匹配对象 [root@saltserver pillar]# salt -I 'conf:/etc/123.conf' test.ping saltminion01.local: True
1.修辑master的配置文件
[root@saltserver pillar]# vim /etc/salt/master // 取消下面三句的注释 file_roots: base: - /srv/salt // 建立/srv/salt目录 [root@saltserver pillar]# mkdir /srv/salt/ [root@saltserver pillar]# cd /srv/salt/
2.建立/srv/salt/top.sls文件
[root@saltserver salt]# vim top.sls base: '*': - httpd // 重启master [root@saltserver salt]# /etc/init.d/salt-master restart Stopping salt-master daemon: [ OK ] Starting salt-master daemon: [ OK ]
3.新建/srv/salt/httpd.sls文件
[root@saltserver salt]# vim /srv/salt/httpd.sls [root@saltserver salt]# vim httpd.sls install_httpd: pkg.installed: - names: - httpd - httpd-devel service.running: - name: httpd - enable: True
4.测试安装(以saltminion.local为例)
[root@saltserver salt]# salt 'saltminion.local' state.highstate saltminion.local: ---------- ID: install_httpd Function: pkg.installed Name: httpd Result: True Comment: Package httpd is already installed. Started: 00:17:18.266735 Duration: 1064.537 ms Changes: ---------- ID: install_httpd Function: pkg.installed Name: httpd-devel Result: True Comment: The following packages were installed/updated: httpd-devel Started: 00:17:19.331436 Duration: 33639.881 ms Changes: ---------- apr: ---------- new: 1.3.9-5.el6_9.1 old: 1.3.9-5.el6_2 apr-devel: ---------- new: 1.3.9-5.el6_9.1 old: apr-util-devel: ---------- new: 1.3.9-3.el6_0.1 old: cyrus-sasl-devel: ---------- new: 2.1.23-15.el6_6.2 old: db4: ---------- new: 4.7.25-22.el6 old: 4.7.25-20.el6_7 db4-cxx: ---------- new: 4.7.25-22.el6 old: db4-devel: ---------- new: 4.7.25-22.el6 old: db4-utils: ---------- new: 4.7.25-22.el6 old: 4.7.25-20.el6_7 expat: ---------- new: 2.0.1-13.el6_8 old: 2.0.1-11.el6_2 expat-devel: ---------- new: 2.0.1-13.el6_8 old: httpd-devel: ---------- new: 2.2.15-69.el6.centos old: openldap: ---------- new: 2.4.40-16.el6 old: 2.4.40-12.el6 openldap-devel: ---------- new: 2.4.40-16.el6 old: ---------- ID: install_httpd Function: service.running Name: httpd Result: True Comment: Service httpd has been enabled, and is running Started: 00:17:53.015925 Duration: 670.976 ms Changes: ---------- httpd: True Summary ------------ Succeeded: 3 (changed=2) Failed: 0 ------------ Total states run: 3 // 客户端上检查 [root@saltminion ~]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME httpd 7505 root 4u IPv6 43159 0t0 TCP *:http (LISTEN) httpd 7507 apache 4u IPv6 43159 0t0 TCP *:http (LISTEN) httpd 7508 apache 4u IPv6 43159 0t0 TCP *:http (LISTEN) httpd 7509 apache 4u IPv6 43159 0t0 TCP *:http (LISTEN) httpd 7510 apache 4u IPv6 43159 0t0 TCP *:http (LISTEN) httpd 7511 apache 4u IPv6 43159 0t0 TCP *:http (LISTEN) httpd 7512 apache 4u IPv6 43159 0t0 TCP *:http (LISTEN) httpd 7513 apache 4u IPv6 43159 0t0 TCP *:http (LISTEN) httpd 7514 apache 4u IPv6 43159 0t0 TCP *:http (LISTEN)
1.新建/srv/salt/test.sls
[root@saltserver salt]# vim /srv/salt/test.sls // 添加以下内容 // 说明:第一行的file_test为自定的名字,表示该配置段的名字,能够在别的配置段中引用它,source指定文件从哪里拷贝, // 这里的salt://test/123/1.txt至关因而/srv/salt/test/123/1.txt file_test: file.managed: - name: /tmp/aminglinux.com - source: salt://test/123/1.txt - user: root - group: root - mode: 600 [root@saltserver salt]# mkdir -p test/123 [root@saltserver salt]# echo '11111' >> test/123/1.txt [root@saltserver salt]# cat !$ cat test/123/1.txt 11111
2.编辑/srv/salt/top.sls文件
[root@saltserver salt]# vim top.sls base: '*': - test
3.同步测试
[root@saltserver salt]# salt 'saltminion01.local' state.highstate saltminion01.local: ---------- ID: file_test Function: file.managed Name: /tmp/aminglinux.com Result: True Comment: File /tmp/aminglinux.com updated Started: 13:08:40.688514 Duration: 18.984 ms Changes: ---------- diff: New file Summary ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1 // saltminion01.local查看文件是否同步过去 [root@saltminion01 ~]# cat /tmp/aminglinux.com 11111 [root@saltminion01 ~]# ls -l /tmp/aminglinux.com -rw------- 1 root root 6 Sep 5 13:08 /tmp/aminglinux.com
1.在master上新建配置文件/srv/salt/test_dir.sls
[root@saltserver salt]# vim /srv/salt/test_dir.sls // 添加内容以下 file_dir: file.recurse: - name: /tmp/testdir - source: salt://test/123 - user: root - file_mode: 640 - dir_mode: 750 - mkdir: True - clean: True // clean:True表示源删除文件或目录,目标也会跟着删除,不然不会
2.修改top.sls
[root@saltserver salt]# vim top.sls base: '*': - test_dir
3.同步测试
[root@saltserver salt]# salt 'saltminion01.local' state.highstate saltminion01.local: ---------- ID: file_dir Function: file.recurse Name: /tmp/testdir Result: True Comment: Recursively updated /tmp/testdir Started: 13:16:30.379320 Duration: 1044.869 ms Changes: ---------- /tmp/testdir: ---------- mode: 0750 /tmp/testdir/1.txt: ---------- diff: New file mode: 0640 Summary ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1 // 在saltminion01.local查看目录是否同步成功 drwxr-x--- 2 root root 4096 Sep 5 13:16 testdir [root@saltminion01 ~]# ls -lR /tmp/ /tmp/: total 8 -rw------- 1 root root 6 Sep 5 13:08 aminglinux.com drwxr-x--- 2 root root 4096 Sep 5 13:16 testdir /tmp/testdir: total 4 -rw-r----- 1 root root 6 Sep 5 13:16 1.txt // 注意,若是目录为空,则不会同步
1.新建配置文件/srv/salt/shell_test.sls
[root@saltserver salt]# vim /srv/salt/shell_test.sls // 内容以下 shell_test: cmd.script: - source: salt://test/1.sh - user: root
2.在source目录下新建脚本
[root@saltserver salt]# vim /srv/salt/test/1.sh #!/bin/bash echo "haha"
3.修改top.sls内容
"test/1.sh" [New] 3L, 25C written [root@saltserver salt]# vim top.sls base: '*': - shell_test
4.测试
"top.sls" 3L, 30C written [root@saltserver salt]# salt 'saltminion01.local' state.highstate saltminion01.local: ---------- ID: shell_test Function: cmd.script Result: True Comment: Command 'shell_test' run Started: 13:27:50.265831 Duration: 146.006 ms Changes: ---------- pid: 10120 retcode: 0 stderr: stdout: haha Summary ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1
1.新建/srv/salt/cron_test.sls文件
Total states run: 1 [root@saltserver salt]# vim /srv/salt/cron_test.sls // 添加以下内容 cron_test: cron.present: - name: /bin/touch /tmp/111.txt - user: root - minute: '*' - hour: 20 - daymonth: '*' - month: '*' - dayweek: '*' // 注意,*须要用单引号引发来。固然咱们还可使用file.managed模块来管理cron,由于系统的cron都是以配置文件的形式存在的。
2.编辑top.sls文件
[root@saltserver salt]# vim top.sls base: '*': - cron_test
3.测试
// 在master端推送 [root@saltserver salt]# salt 'saltminion01.local' state.highstate saltminion01.local: ---------- ID: cron_test Function: cron.present Name: /bin/touch /tmp/111.txt Result: True Comment: Cron /bin/touch /tmp/111.txt added to root's crontab Started: 13:59:11.007707 Duration: 19.99 ms Changes: ---------- root: /bin/touch /tmp/111.txt Summary ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1 //在saltminion01.local查看计划任务,不要手动修改salt管理的crontab任务,不然就无法删除或者修改这个cron了 [root@saltminion01 ~]# crontab -e # Lines below here are managed by Salt, do not edit # SALT_CRON_IDENTIFIER:/bin/touch /tmp/111.txt * 20 * * * /bin/touch /tmp/111.txt
4.删除任务计划
// 修改corn_test.sls // 二者不能共存,要想删除一个cron,那以前的present就得去掉。 [root@saltserver salt]# vim cron_test.sls cron_test: cron.absent: - name: /bin/touch /tmp/111.txt
5.同步配置文件
[root@saltserver salt]# salt 'saltminion01.local' state.highstate saltminion01.local: ---------- ID: cron_test Function: cron.absent Name: /bin/touch /tmp/111.txt Result: True Comment: Cron /bin/touch /tmp/111.txt removed from root's crontab Started: 14:04:29.354015 Duration: 12.691 ms Changes: ---------- root: /bin/touch /tmp/111.txt Summary ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1
6.在saltminion01.local上检查
[root@saltminion01 ~]# crontab -e // 已经删除了 # Lines below here are managed by Salt, do not edit ~
1.拷贝master上的文件到客户端
// cp.get_file [root@saltserver salt]# salt '*' cp.get_file salt://test/1.sh /tmp/1.sh saltminion01.local: /tmp/1.sh saltminion.local: /tmp/1.sh saltserver: /tmp/1.sh //验证 [root@saltminion01 ~]# ls -l /tmp/1.sh -rw-r--r-- 1 root root 25 Sep 5 14:08 /tmp/1.sh
2.拷贝master上的目录到客户端
// cp.get_dir [root@saltserver salt]# salt '*' cp.get_dir salt://test/123 /tmp/ saltminion01.local: - /tmp//123/1.txt saltserver: - /tmp//123/1.txt saltminion.local: - /tmp//123/1.txt //在minion端验证 [root@saltminion01 ~]# ls -lR /tmp/ /tmp/: total 16 drwxr-xr-x 2 root root 4096 Sep 5 14:20 123 -rw-r--r-- 1 root root 25 Sep 5 14:08 1.sh -rw------- 1 root root 6 Sep 5 13:08 aminglinux.com drwxr-x--- 2 root root 4096 Sep 5 13:16 testdir /tmp/123: total 4 -rw-r--r-- 1 root root 6 Sep 5 14:20 1.txt /tmp/testdir: total 4 -rw-r----- 1 root root 6 Sep 5 13:16 1.txt
3.显示存活的minion
//salt-run manage.up [root@saltserver salt]# salt-run manage.up - saltminion.local - saltminion01.local - saltserver
4.命令行下执行master上的shell脚本
//cmd.script [root@saltserver salt]# salt '*' cmd.script salt://test/1.sh saltminion01.local: ---------- pid: 10281 retcode: 0 stderr: stdout: haha saltserver: ---------- pid: 80391 retcode: 0 stderr: stdout: haha saltminion.local: ---------- pid: 9392 retcode: 0 stderr: stdout: haha
1.安装salt-ssh
[root@saltserver salt]# yum -y install salt-ssh
2.新建配置文件 /etc/salt/roster
[root@saltserver salt]# vim /etc/salt/roster saltminion.local: host: 192.168.1.43 user: root passwd: 123456 saltminion01.local: host: 192.168.1.42 user: root passwd: 123456
3.推送key
// 第一次推送不成功 [root@saltserver salt]# salt-ssh --key-deploy '*' -r 'w' saltminion.local: ---------- retcode: 254 stderr: stdout: The host key needs to be accepted, to auto accept run salt-ssh with the -i flag: The authenticity of host '192.168.1.43 (192.168.1.43)' can't be established. RSA key fingerprint is 3d:d4:e5:45:01:72:0e:15:2f:43:2f:6a:2e:c6:77:a7. Are you sure you want to continue connecting (yes/no)? saltminion01.local: ---------- retcode: 254 stderr: stdout: The host key needs to be accepted, to auto accept run salt-ssh with the -i flag: The authenticity of host '192.168.1.42 (192.168.1.42)' can't be established. RSA key fingerprint is 3d:d4:e5:45:01:72:0e:15:2f:43:2f:6a:2e:c6:77:a7. Are you sure you want to continue connecting (yes/no)? //须要登陆一次 [root@saltserver salt]# ssh saltminion01.local The authenticity of host 'saltminion01.local (192.168.1.42)' can't be established. RSA key fingerprint is 3d:d4:e5:45:01:72:0e:15:2f:43:2f:6a:2e:c6:77:a7. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'saltminion01.local' (RSA) to the list of known hosts. root@saltminion01.local's password: Last login: Sat Sep 1 23:33:29 2018 from 192.168.1.9 [root@saltminion01 ~]# ssh saltminion.local The authenticity of host 'saltminion.local (192.168.1.43)' can't be established. RSA key fingerprint is 3d:d4:e5:45:01:72:0e:15:2f:43:2f:6a:2e:c6:77:a7. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'saltminion.local,192.168.1.43' (RSA) to the list of known hosts. root@saltminion.local's password: Last login: Sat Sep 1 23:33:40 2018 from 192.168.1.9 [root@saltserver salt]# salt-ssh --key-deploy '*' -r 'w' saltminion01.local: ---------- retcode: 0 stderr: stdout: 14:48:01 up 4 days, 15:41, 2 users, load average: 0.07, 0.03, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root tty1 - Sat09 3days 0.04s 0.04s -bash root pts/1 192.168.1.9 Sat23 5:19 0.15s 0.15s -bash saltminion.local: ---------- retcode: 0 stderr: stdout: 14:48:10 up 4 days, 15:41, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root tty1 - Sat09 3days 0.00s 0.00s -bash root pts/1 192.168.1.9 Sat23 5:38 0.04s 0.04s -bash // 删除roster中用户的密码,再次测试 [root@saltserver salt]# salt-ssh '*' -r 'w' saltminion.local: ---------- retcode: 0 stderr: stdout: 14:49:55 up 4 days, 15:43, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root tty1 - Sat09 3days 0.00s 0.00s -bash root pts/1 192.168.1.9 Sat23 7:23 0.04s 0.04s -bash saltminion01.local: ---------- retcode: 0 stderr: stdout: 14:49:46 up 4 days, 15:42, 2 users, load average: 0.01, 0.02, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root tty1 - Sat09 3days 0.04s 0.04s -bash root pts/1 192.168.1.9 Sat23 7:04 0.15s 0.15s -bash