yum install salt-master salt-minion salt-ssh \
salt-syndic salt-cloud salt-api -y
sed -i 's/#master: salt/master: 127.0.0.1/g' /etc/salt/minion
grep -r ^file_roots /etc/salt/master || cat > /etc/salt/master<< EOF
auto_accept: True
hash_type: sha256
#指定files和pillar环境的路径
file_roots:
base:
- /srv/salt/base
dev:
- /srv/salt/dev
prod:
- /srv/salt/prod
pillar_roots:
live:
- /srv/salt/pillar/live
game:
- /srv/salt/pillar/game
#主机分组
nodegroups:
node: 'E@node'
web: 'E@nginx'
zk: 'E@zookeeper*'
redis: 'E@redis*'
mongo: 'E@mongo*'
redis_and_mongo: 'E@mongodb* and E@redis*'
apps: '* and not N@web and not N@zk and not N@es and not N@mongodb and not N@redis'
#salt -N apps test.ping #检测应用虚拟机
EOF
systemctl enable salt-master ; chkconfig salt-master on
systemctl enable salt-minion ; chkconfig salt-minion on
systemctl restart salt-master ; service salt-master restart
systemctl restart salt-minion ; service salt-minion restart
#yum install https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm
#centos7 python2
#yum install https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el6.noarch.rpm
#centos6 python
#yum install https://repo.saltstack.com/py3/redhat/salt-py3-repo-latest-2.el7.noarch.rpm
#centos7 python3
yum remove salt-minion -y &&rm /etc/salt/pki/minion/ -rf # 清理key
yum install salt-minion -y
[ -f "/etc/salt/minion" ] && cat > /etc/salt/minion <<EOF
master: 192.168.0.11 #ip,dns
id: $HOSTNAME
tcp_keepalive: True
tcp_keepalive_idle: 300
tcp_keepalive_cnt: -1
tcp_keepalive_intvl: -1
EOF
chkconfig salt-minion on && service salt-minion restart
#systemctl enable salt-minion && systemctl restart salt-minion
同步配置
salt rsync -av /etc/salt/master ops-salt-2:/etc/salt/
同步文件
salt rsync -av /srv ops-salt-2:/
同步key
salt rsync -av /etc/salt/pki/master ops-salt-2:/etc/salt/pki
minion端
/etc/salt/minion
master:
- ops-salt-1
- ops-salt-2
生产要确保配置统一,使用rsync触发同步
yum install salt-syndic
/etc/salt/master (master)
order_masters: True
# master端允许开启多层master
/etc/salt/master (syndic)
syndic_master: 10.4.xx.xx
# syndic端指定master ip
mkdir /etc/salt/pki/master/ssh #创建salt-ssh存放目录
cat roster #推荐使用key方便密码管理
account-1-3:
host: 10.x.x.3
user: super
sudo: True
客户端推送key #可以和jumpserver共用
adduser super
su - super -c 'mkdir -p /home/super/.ssh; \
echo >/home/super/.ssh/authorized_keys; \
echo "ssh-rsa AAAAB*******rsQ== super@jsm-xx">>/home/super/.ssh/authorized_keys; \
chmod 700 /home/super/.ssh; \
chmod 600 /home/super/.ssh/authorized_keys;'
(grep "super" -lr /etc/sudoers)||cat >> /etc/sudoers << EOF
super ALL=(ALL) NOPASSWD: ALL
Defaults:super !requiretty
EOF
初始化客户端
salt-ssh -i '*' state.sls base.init.env_init
#初始化,并按照salt-minion
salt '*' state.sls base.init.zabbix.service pillarenv="live"
#直播环境装zabbix,渲染hostname;live-redis-1
调用本地模块:
salt-call --local cmd.run "uptime"