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


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

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

base:
  'roles:nginx':
    - match: grain
    - init.pkg
    - init.limit


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

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

#!/bin/bash
hostname


roster文件:cat /etc/salt/roster

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

#!/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

test1.discuz.com root redhat
test2.discuz.com root redhat
tomcat1.discuz.com root centos
tomcat2.discuz.com root centos