记得很乱,需再整理
在master端重启minion
salt '*' service.restart salt-minion
利用Returner_Mysql
需要在每台minion上配置,vim /etc/salt/minion
每个minion都需要安装python连接mysql的模块:
yum -y install MySQL-python
mysql.host: '192.168.99.47'
mysql.user: 'salt'
mysql.pass: 'salt123'
mysql.db: 'salt'
mysql.port: 3306
在mysql上
DROP TABLE IF EXEISTS 'salt_returns';
CREATE TABLE 'salt_returns' (
'fun' varchar(50) not null,
'jid' varchar(255) not null,
'id' mediumtext not null,
'success' varchar(10) not null,
'full_ret' mediumtext not null,
'alter_time' timestamp default current_timestamp,
key 'id' ('id'),
key 'jid' ('jid'),
key 'fun' ('fun')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
grant all on salt.* to salt@'192.168.99.%' identified by "salt123";
flush privileges;
event
event是一个本地的ZeroMQ PUB Interface,event是一个开放的系统,用于发送信息
通知salt或其他的操作系统
每个event都有一个标签。事件标签允许快速置顶过滤事件。除了标签之外,每个事件都有一个数据结构。这个数据结构是一个dict类型,其中包含关键事件的信息。
安装mysql
创建数据库,授权
修改master配置文件/etc/salt/master
mysql.host: '192.168.99.47'
mysql.user: 'salt'
mysql.pass: 'salt123'
mysql.db: 'salt'
mysql.port: 3306
4.准备监听event的py脚本
5.安装python的mysql模块
在master端
import salt.utils.event
event = salt.utils.event.MasterEvent('/var/run/salt/master')
print eachevent
print '------'
yum -y install MySQL-python
DROP TABLE IF EXEISTS 'salt_returns';
CREATE TABLE 'salt_returns' (
'fun' varchar(50) not null,
'jid' varchar(255) not null,
'id' mediumtext not null,
'success' varchar(10) not null,
'full_ret' mediumtext not null,
'alter_time' timestamp default current_timestamp,
key 'id' ('id'),
key 'jid' ('jid'),
key 'fun' ('fun')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
grant all on salt.* to salt@'192.168.99.%' identified by "salt123";
flush privileges;
vi salt_event_to_mysql.py
#!/usr/bin/env python
#coding=utf8
import json
import salt.config
import salt.utils.event
import MySQLdb
__opts__ salt.config.client('/etc/salt/master')
conn = MySQLdb.connect(host=__opts__['mysql.host',user=__opts__['mysql.user'],passwd=__opts__['mysql.pass'],db=__opts__['mysql.db'],port=__opts__['mysql.port'])
cursor = conn.cursor()
event = salt.utils.event.iter_events(full=True):
ret = eachevent['data']
if "salt/job/" in eachevent['tag']:
if ret.has_key('id') and ret.has_key('return'):
if ret['fun'] == "saltutil.find_job":
continue
sql = '''INSERT INOT 'salt_returns' ('fun', 'jid', 'return','id','success','full_ret') VALUES (%s, %s, %s, %s, %s, %s)'''
cursor.execute(sql, (ret['fun'],ret['jid'],json.dumps(ret['return']),ret['id'],ret['success'],json.dumps(ret)))
cursor.execute("COMMIT")
else:
pass
分组:
vim /etc/salt/master
nodegroups:
group1: 'E@salt-client*'
group2: 'E@salt-pay*'
YAML规则:每个group前边有2个空格,冒号(:)后边1个空格
salt模块,只是了解一下,需要找详细的资料看
文件系统
修改配置文件
创建目录
写sls文件
vim /etc/salt/master
file_roots:
base:
- /srv/salt/
# mkdir -p /srv/salt
cd /srv/salt
mkdir /etc
cd /etc/
mkdir script
vi test.sh
#!/bin/bash
while true
do
sleep 1
echo 1 >>/tmp/log
done
salt '*' cmd.script salt://etc/script/test.sh
1种
vim /srv/salt/hosts.sls
/tmp/hosts:
file.managed:
- source: salt://etc/hosts
- user: root
- group: root
- mode: 600
salt '*' state.highstate
2种
vim /srv/salt/top.sls
base:
'*':
- hosts
salt '*' state.sls hosts
3种
cd /srv/salt
mkdir hosts
mv hosts.sls ./hosts/
salt '*' state.sls hosts.hosts
grains
在minion上进行配置grains
vi /etc/salt/minion
grains:
roles:
- webserver
- memcache
deployment: datacenter4
cabinet: 13
cab_u: 14-15
/etc/init.d/salt-minion restart
states
salt状态系统的核心是sls,或者叫**S**alt State文件。SLS表示
系统将会是什么样的一种状态,而且是以一种很简单的格式来包含这些数据,
这经常也被叫做配置管理。
SLS文件实际上只是一些:字典dictionaries,列表lists,
字符串,and数字
yaml
缩进:yaml使用一个固定的缩进风格表示数据层结构关系。
salt需要每个缩进级别由两个空格组成。不要使用tabs
python的字典理所当然是简单的键值对。字典的keys在yaml中的
表现形式是一个以冒号结尾的字符串。values的表现形式冒号下面的每一行,用一个空格隔开
想要表示列表项,使用一个短横杠加一个空格。多个项使用同样
的缩进级别作为同一列表的一部分
http://docs.saltstack.cn/topics/yaml/index.html
http://docs.saltstack.com/en/latest/ref/states/all/index.html
练习
使用sls安装nginx,并管理nginx的配置文件,
当nginx配置文件被修改时,自动更新配置文件,
并重启nginx
pkg
vim /srv/salt/nginx/init.sls
nginx:
pkg:
- installed
service:
- running
- enable: True
- reload: True
- watch:
- pkg: nginx
- file: /etc/nginx/nginx.conf
- file: /etc/nginx/conf.d/default.conf
/etc/nginx/nginx.conf:
file.managed:
- source: salt://etc/nginx/nginx.conf
- user: root
- group: root
- mode: 644
/etc/nginx/conf.d/default.conf:
file.managed:
- source: salt://etc/nginx/conf.d/default.conf
- user: root
- group: root
- mode: 644
mkdir /srv/salt/etc/nginx/conf.d -p
cp /etc/nginx/nginx.conf /srv/salt/etc/nginx/
cp /etc/nginx/conf.d/nginx.conf /srv/salt/etc/nginx/conf.d/
salt '*' state.sls nginx
z在minion端
salt-call state.sls nginx
syndic
syndic_master: 更高级别master的ip地址
syndic_log_file: syndic日志文件的路径(绝对路径或相对路径均可)
order_master: True
yum -y install salt-master
vi /etc/salt/master
syndic_master: 192.168.99.40
/etc/init.d/salt-syndic start
/etc/init.d/salt-master restart