使用salt-ssh批量管理主机

安装salt-ssh:yum install -y salt-master salt-ssh


master配置文件:cat /etc/salt/master

1
2
3
4
5
6
7
8
9
file_roots:
  base:
    - /srv/salt/
  dev:
    - /srv/salt/dev/services
    - /srv/salt/dev/states
  prod:
    - /srv/salt/prod/services
    - /srv/salt/prod/states

top.sls文件:cat /srv/salt/top.sls

1
2
3
4
5
base:
  'roles:nginx':
    - match: grain
    - init.pkg
    - init.limit


limit文件:cat /srv/salt/init/limit.sls

1
2
3
4
5
6
7
8
9
10
11
12
limit-conf-config:
  file.managed:
    - name: /tmp/limits.conf
    - source: salt://init/files/limits.conf
    - user: root
    - group: root
    - mod: 644
    - name: /tmp/df.sh
    - source: salt://init/files/df.sh
    - user: root
    - group: root
    - mod: 644


files目录:ls /srv/salt/init/files

df.sh  limits.conf


df.sh文件:cat /srv/salt/init/files/df.sh

1
2
#!/bin/bash
hostname


roster文件:cat /etc/salt/roster

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
test1.discuz.com:
  host: test1.discuz.com
  user: root
  passwd: redhat
test2.discuz.com:
  host: test2.discuz.com
  user: root
  passwd: redhat
tomcat1.discuz.com:
  host: tomcat1.discuz.com
  user: root
  passwd: centos
tomcat2.discuz.com:
  host: tomcat2.discuz.com
  user: root
  passwd: centos

  

传送文件到各主机:salt-ssh '*' state.sls init.limit

在各主机执行脚本:salt-ssh '*' cmd.run 'bash /tmp/df.sh'


生成roster脚本

roster.sh文件:cat /root/roster.sh

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
>/etc/salt/roster
IFS=' '
cat /root/hosts while read line
do
   arr=($line)
   echo ${arr[0]}":">>/etc/salt/roster
   echo "  host: "${arr[0]}>>/etc/salt/roster
   echo "  user: "${arr[1]}>>/etc/salt/roster
   echo "  passwd: "${arr[2]}>>/etc/salt/roster
done


hosts文件:cat /root/hosts

1
2
3
4
test1.discuz.com root redhat
test2.discuz.com root redhat
tomcat1.discuz.com root centos
tomcat2.discuz.com root centos



     本文转自1321385590 51CTO博客,原文链接:http://blog.51cto.com/linux10000/1894406,如需转载请自行联系原作者








上一篇:字符串匹配算法KMP算法


下一篇:扩展ViewFlow避免和ViewPager滑动冲突,同时支持无限循环,并完美和CircleFlowIndicator结合