saltstack安装和配置
[root@web9 salt]# vi filetest.sls //ADD file_test: file.managed: - name: /tmp/lulu.com - source: salt://test/123/1.txt - user: root - group: root - mode: 600 /* file_test : 自定义的名字,可在别的配置中引用 source : 指定文件从何处拷贝 */ [root@web9 salt]# mkdir test [root@web9 salt]# mkdir test/123 [root@web9 salt]# vi test/123/1.txt //ADD jsdhjsdhk sadjhjsad [root@web9 salt]# vi top.sls //ADD ... ... - filetest[root@web9 salt]# salt 'web10.lulu.com' state.highstate
一、安装slatstack准备
//prepared 1. 两台机器 172.7.15.106 172.7.15.111 2.设置hostname以及vi /etc/hosts (106) web9.lulu.com (111) web10.lulu.com 3.关闭防火墙 setenforce 0 iptables -F //106 [root@web9 ~]# yum install -y epel-release [root@web9 ~]# yum install -y salt-master salt-minion //111 [root@web10 ~]# yum install -y epel-release [root@web10 ~]# yum install -y salt-minion
//START SERVICE //106 [root@web9 ~]# vi /etc/salt/minion //almost 16 colum change or add master: 172.7.15.106 [root@web9 ~]# service salt-master start [root@web9 ~]# service salt-minion start //111 [root@web10 ~]# vi /etc/salt/minion //almost 16 colum change or add master: 172.7.15.106 [root@web10 ~]# service salt-minion start
二、配置认证
//106 [root@web9 ~]# salt-key //这个命令可查看到是否已签名的客户端 Accepted Keys: Denied Keys: Unaccepted Keys: web10.lulu.com web9.lulu.com Rejected Keys: [root@web9 ~]# salt-key -a web10.lulu.com
[root@web9 ~]# salt-key -a web9.lulu.com --此处为方便后面实验的操作{可不做}
//111 [root@web10 ~]# ls /etc/salt/pki/minion minion_master.pub
三、远程执行命令
[root@web9 ~]# salt '*' test.ping web9.lulu.com True web10.lulu.com True /* Here * represents signed client. You can as well specify one of the clients. */ [root@web9 ~]# salt '*' cmd.run 'hostname' web9.lulu.com web9.lulu.com web10.lulu.com web10.lulu.com /* Here * must be a accepted client. You can look over the client through any cmd. */ [root@web9 ~]# salt -E 'web(9|10)' cmd.run 'hostname' web9.lulu.com web9.lulu.com web10.lulu.com web10.lulu.com /* 使用正则 --必须加 -E选项 */ [root@web9 ~]# salt -L 'web9.lulu.com,web10.lulu.com' cmd,run 'hostname' web9.lulu.com web9.lulu.com web10.lulu.com web10.lulu.com /* 列表--多个机器用逗号分隔*/
四、grains
[root@web9 ~]# salt 'web10.lulu.com' grains.ls //列出所有的grains项目名字 [root@web9 ~]# salt 'web10.lulu.com'grains.items //列出所有的grains项目以及值
//自定义grains //111 [root@web10 ~]# vim /etc/salt/grains //ADD ... role: nginx env: test [root@web10 ~]# service salt-minion restart //106 /*确认*/ [root@web9 ~]# salt '*' grains.ls //找看看是否生成了新的grains [root@web9 ~]# salt '*' grains.item //看看值是否定义的值 /*获取并执行*/ //借助grains属性信息---找到客户端 ---然后执行命令 [root@web9 ~]# salt '*' grains.item role env //获取grains [root@web9 ~]# salt -G role:nginx cmd.run 'hostname'
web10.lulu.com
web10.lulu.com
五、pillar
//pillar [root@web9 ~]# salt '*' pillar.item //查看是否存在pillar的item //自定义pillar [root@web9 ~]# vim /etc/salt/master //find and change ... ... #pillar_roots: # base: # - /srv/pillar //把#去掉 [root@web9 ~]# mkdir /srv/pillar [root@web9 ~]# vi /srv/pillar/test.sls //ADD conf: /etc/123.conf myname:lulu.com [root@web9 ~]# vi /srv/pillar/top.sls //ADD base: 'web10.lulu.com': - test [root@web9 ~]# service salt-master restart [root@web9 ~]# salt '*' saltutil.refresh_pillar /*当更新完pillar配置文件,通过刷新pillar配置获取新的pillar状态*/ web9.lulu.com True web10.lulu.com True [root@web9 ~]# salt '*' pillar.items web10.lulu.com ---------- conf: /etc/123.conf myname: lulu.com web9.lulu.com ---------- ============== //增加新的pillar [root@web9 ~]# cd /srv/pillar [root@web9 pillar]# ls test.sls top.sls [root@web9 pillar]# vi abc.sls //ADD mydomain:abc.conf [root@web9 pillar]# vi top.sls //ADD ... ... - test - abc [root@web9 pillar]# service salt-master restart [root@web9 pillar]# salt '*' saltutil.refresh_pillar [root@web9 pillar]# salt 'web10.lulu.com' pillar.items web10.lulu.com ---------- conf: /etc/123.conf myname: lulu.com mydomain: abc.conf /*获取并执行*/ [root@web9 pillar]# salt -I mydomain:abc.conf cmd.run 'hostname' web10.lulu.com web10.lulu.com
六、配置管理安装Apache
//106 [root@web9 ~]# vi /etc/salt/master //change #file_roots: # base" # - /srv/salt //将#去掉 [root@web9 ~]# mkdir /srv/salt [root@web9 ~]# cd /srv/salt [root@web9 salt]# vi /srv/salt/top.sls //ADD base: '*': - apache /* 让所有的客户端上执行apache模块*/ [root@web9 salt]# service salt-master restart [root@web9 salt]# vi apache.sls //ADD apache-service: pkg.installed: - names: - httpd - httpd-devel service.running: - name: httpd - enable: True /*说明: apache-service : id 的名字,(自定义) pkg.installed : 包安装函数,此处下方跟着要安装的包的名字 -names : 如果此处只有一个服务,则写成-name: httpd 。不需要换行 service.running : 函数,保证指定的服务启动 enabled : 表示开机自启动 */ [root@web9 salt]# salt 'web10.lulu.com' state.highstate
//111
[root@web10 ~]# ps aux|grep httpd //查看是否启动了httpd服务
七、配置管理文件
[root@web9 salt]# vi filetest.sls //ADD file_test: file.managed: - name: /tmp/lulu.com - source: salt://test/123/1.txt - user: root - group: root - mode: 600 /* file_test : 自定义的名字,可在别的配置中引用 source : 指定文件从何处拷贝 */ [root@web9 salt]# mkdir test [root@web9 salt]# mkdir test/123 [root@web9 salt]# vi test/123/1.txt //ADD jsdhjsdhk sadjhjsad [root@web9 salt]# vi top.sls //ADD ... ... - filetest
[root@web9 salt]# salt 'web10.lulu.com' state.highstate
八、配置管理目录
[root@web9 salt]# vi dirtest.sls //ADD file_dir: file.recurse: - name: /tmp/testdir - source: salt://test/123 - user: root - file_mode: 644 - dir_mode: 755 - mkdir: True - clean: True /* clean:True -- 加上后,源删除文件或目录,目标也会跟着删除 */ [root@web9 salt]# vi top.sls //ADD ... ... ... - dirtest [root@web9 salt]# salt 'web10.lulu.com' state.highstate [root@web9 salt]# touch test/123/lu.txt [root@web9 salt]# salt 'web10.lulu.com' state.highstate //生成lu.txt
九、配置管理远程命令
[root@web9 salt]# vi cmdtest.sls //ADD cmd_test: cmd.run: - unless: test -f /tmp/linux.txt - names: - touch /tmp/111.txt - mkdir /tmp/123 - user: root [root@web9 salt]# vi top.sls //ADD ... ... ... ... - cmdtest //client [root@web10 ~]# rm -rf /tmp/ //server [root@web9 salt]# salt 'web10.lulu.com' state.highstate //client [root@web10 ~]# ls /tmp/ 111.txt 123 //换成onlyif看效果 [root@web9 salt]# vi cmdtest.sls //change ... ... unless:-->onlyif [root@web9 salt]# salt 'web10.lulu.com' state.highstate
十、远程执行shell脚本
[root@web9 salt]# vi shelltest.sls //ADD shell_test: cmd.script: - source: salt://test/1.sh - user: root [root@web9 salt]# vi /srv/salt/test.1.sh //ADD #!/bin/bash touch /tmp/111.txt if [ -d /tmp/1233] then rm -rf /tmp/1233 else mkdir /tmp/1233 fi [root@web9 salt]# vi top.sls //ADD ... ... ... ... - shelltest [root@web9 salt]# salt 'web10.lulu.com' state.highstate //client [root@web10 ~]# ls /tmp/ 1233 111.txt
十一、管理任务计划
[root@web9 salt]# vi crontest.sls //ADD cron_test: cron.present: - name: /bin/touch /tmp/111.txt - user: root - minute: '*' - hour: 20 - daymonth: '*' - month: '*' - dayweek: '*' /* 1. *需要用' '括起来 2. 删除该cron,增加: cron.absent: - name: /bin/touch /tmp/111.txt 3.cron.present和cron.absent不可同时存在 PS: 可用file.managed模块管理cron */ [root@web9 salt]# vi top.sls //ADD ... ... ... ... ... - crontest //client [root@web10 ~]# crontab -l
十二、常用命令
1. cp.get_file --> 拷贝master上的文件到客户端 //使用方法 --拷贝文件 [root@web9 salt]# ls apache.sls [root@web9 salt]# salt 'web10.lulu.com' cp.get_file salt://apache.sls /tmp/lulu.com web10.lulu.com /tmp/lulu.com /* 解释: salt://apache.sls -- > 将当前目录的apache.sls -- > /tmp/lulu.com --> 拷贝到client的 /tmp/lulu.com 里 */ 2. cp.get_dir --> 拷贝master上的目录到客户端 --拷贝目录 [root@web9 salt]# salt 'web10.lulu.com' cp.get_dir salt://test /tmp/lulu web10.lulu.com - /tmp/lulu/test/1.sh - /tmp/lulu/test/123/1.txt - /tmp/lulu/test/123/linux.txt 3. salt-run manage.up --> 显示存活的minion(客户端) [root@web9 ~]# salt-run manage.up - web10.lulu.com - web9.lulu.com