2018年7月3日笔记
- state
1)state介绍
state是saltstack最核心的功能,通过预先指定好的sls文件对minion进行状态管理,支持pkg, file, network, service, user等。
2)配置管理 nginx
vim /etc/salt/master
找到:file_roots:
在file_roots目录下,vim top.sls
base:
'*':
- nginx
vim nginx.sls
nginx-service:
pkg:
- installed
service:
- running
- require:
- pkg: nginx
- enable: True
nginx-service:是id名字,自定义。pkg: 函数 installed:代表没有安装的通过yum或者apt-get安装,running:启动,enable:开机启动:require:确保只有在安装成功后才会启动。
执行state:
salt '*' state.highstate
3)配置管理文件
file_test:
file.managed:
- name: /tmp/aminglinux.com
- source: salt://test/123/1.txt
- user: root
- group: root
- mode: 644
说明:第一行的file_test为自定的名字,表示该配置段的名字,可以在别的配置段中引用它,source指定文件从哪里拷贝,这里的test相当于是/srv/salt/test
4)配置管理目录
file_dir:
file.recurse:
- name: /tmp/testdir
- source: salt://test/123
- user: root
- file_mode: 644
- dir_mode: 755
- mkdir: True
- clean: True //加上它之后,源删除文件或目录,目标也会跟着删除,否则不会删除
5)配置管理远程命令
cmd_test:
cmd.run:
- onlyif: test -f /tmp/111.txt
- names:
- touch /tmp/111.txt
- mkdir /tmp/1233
- user: root
还可以加unless,和onlyif正好相反
6)配置管理远程脚本
shell_test:
cmd.script:
- source: salt://test/1.sh
- user: root
cat /srv/salt/test/1.sh
#!/bin/bash
touch /tmp/111.txt
if [ -d /tmp/1233 ]
then
rm -rf /tmp/1233
fi
7)配置管理计划任务
cron_test:
cron.present:
- name: /bin/touch /tmp/111.txt
- user: root
- minute: '*'
- hour: 20
- daymonth: '*'
- month: '*'
- dayweek: '*'
注意,*需要用单引号引起来。当然我们还可以使用file.managed模块来管理cron,因为系统的cron都是以配置文件的形式存在的。想要删除该cron,需要增加:
cron.absent:
- name: /bin/touch /tmp/111.txt
两者不能共存,要想删除一个cron,那之前的present就得去掉。
- saltstack常用模块和API调用
1)API:(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节。
api的使用原理是通过调用master client模块,实例化一个LocalClient对象,在调用cmd()方法来实现。
以下是API实现test.ping的示例:
yum install -y salt-api
service salt-api restart
import salt.client
client = salt.client.LocalClient()
ret = client.cmd('*', 'test.ping')
print ret
返回形式是一个字典的形式返回
2)cmd模块
salt '*' cmd.run "free -m"
API:client.cmd('wms5test1.app.*', 'cmd.run', ['free -m'])
3)cp模块
file_roots:
base:
- /export/salt/root
salt根目录:在master中file_roots定义的路径,salt://test.txt相当于/export/salt/root/test.txt
salt 'wms5test1.app.172.17.23.176' cp.get_file salt://nscd.conf /tmp/xiang.txt
salt '*' cp.get_dir salt://test /tmp/ (目录)
API:client.cmd('*', 'cp.get_file', ['salt://test.txt', '/tmp/1.txt'])
4)cron模块
salt '*' cron.raw_cron root (查看定时任务)
salt '*' cron.set_job root '*' '*' '*' '*' 1 /export/scripts/rm_log.sh
salt '*' cron.rm_job root /export/scripts/rm_log.sh (写全没效果)
API:client.cmd('*', 'cron.set_job', ['root', '*', '*', '*', '*', 1 /export/scripts/rm_log.sh])
5)dnsutil模块
salt '*' dnsutil.hosts_append /etc/hosts 127.0.0.1 xiang.com
salt '*' dnsutil.hosts_remove /etc/hosts xiang.com
API:client.cmd('*', 'dnsutil.hosts_append', ['/etc/hosts', '127.0.0.1', 'xiang.com'])
6)file模块
salt '*' file.chown /etc/passwd root root
salt '*' file.copy /path/to/src /path/to/dst
salt '*' file.file_exists /etc/hosts
salt '*' file.directory_exists /etc/
salt '*' file.get_mod /etc/passwd
salt '*' file.set_mod /etc/passwd 0644
salt '*' file.mkdir /tmp/test
salt '*' file.sed /export/servers/nginx/conf/nginx.conf 'debug' 'warn'
salt '*' file.append /tmp/test.txt "welcome xiang"
salt '*' file.remove /tmp/1.txt
API:client.cmd('*', 'file.remove', ['/tmp/1.txt'])
7)network模块
salt '*' network.dig www.qq.com
salt '*' network.ping www.qq.com
salt '*' network.ip_addrs
API:client.cmd('*', 'network.ip_addrs')
8)pkg包管理模块
管理yum, apt-get等
salt '*' pkg.install php
salt '*' pkg.remove php
salt '*' pkg.upgrade (升级所有的软件包)
API:client.cmd('*', 'pkg.remove, ['php']')
9)service模块
salt '*' service.enable nginx
salt '*' service.disable nginx
salt '*' service.restart nginx
API:client.cmd('*', 'service.stop', ['nginx'])
- 练习
1)installApp.py
1 def nginx(): 2 resultBean = dict() 3 __salt__['cp.get_file']("salt:_shell/app/nginx.sh /usr/local/src/installnginx.sh") 4 jid = __salt__['cmd.async'] 5 cmd = "sh /usr/local/src/installnginx.sh" 6 status, output = commands.getstatusoutput(cmd) 7 if status == 0: 8 resultBean['code'] = 0 9 resultBean['message'] = 'success' 10 resultBean['data'] = output 11 else: 12 resultBean['code'] = -1 13 resultBean['message'] = 'install nginx error' 14 resultBean['data'] = output 15 return resultBean 16 17 def tomcat(): 18 pass 19 20 def keepalived(): 21 pass 22 23 def lvs(): 24 pass 25 26 def jdk(): 27 pass 28 29 def mysql(): 30 pass 31 32 def zookeeper(): 33 pass 34 35 def redis(): 36 pass
2)saltapi.py
1 import json 2 import requests 3 4 5 class SaltServer(object): 6 def __init__(self): 7 self.session = requests.session() 8 self.token = self.getToken() 9 10 11 def getToken(self): 12 url = "http://192.168.48.135:8000/login" 13 headers = {"Accept": "application/json"} 14 data = { 15 "username": "saltapi", 16 "password": "saltapi", 17 "eauth": "pam" 18 } 19 res = self.session.post(url=url, headers=headers, data=data) 20 text = res.text 21 result = json.loads(text) 22 token = result.get("return")[0].get("token") 23 return token 24 25 def runModules(self, minionid, fun, arg=None): 26 url = "http://192.168.48.135:8000" 27 data = { 28 "client": "local", 29 "tgt": minionid, 30 "fun": fun, 31 "arg": arg 32 } 33 resultBean = dict() 34 try: 35 res = self.session.post(url=url, data=data) 36 text = res.text 37 data = json.loads(text).get("return") 38 resultBean['code'] = 0 39 resultBean['message'] = "success" 40 resultBean['data'] = data 41 except Exception as e: 42 resultBean['code'] = 0 43 resultBean['message'] = "success" 44 resultBean['data'] = e 45 finally: 46 return resultBean 47 48 49 def runRunner(self, fun, arg=None): 50 url = "http://192.168.48.135:8000" 51 data = { 52 "client": "runner", 53 "fun": fun, 54 "arg": arg 55 } 56 resultBean = dict() 57 try: 58 res = self.session.post(url=url, data=data) 59 text = res.text 60 data = json.loads(text).get("return") 61 resultBean['code'] = 0 62 resultBean['message'] = "success" 63 resultBean['data'] = data 64 except Exception as e: 65 resultBean['code'] = 0 66 resultBean['message'] = "success" 67 resultBean['data'] = e 68 finally: 69 return resultBean
3)test.py
1 from lesson31.util.saltapi import SaltServer 2 3 saltServer = SaltServer() 4 5 result1 = saltServer.runModules('192.168.48.129', 'cp.get_file', ['salt://_shell/app/nginx.sh', '/usr/local/src/installnginx.sh']) 6 print(result1) 7 result2 = saltServer.runModules('192.168.48.129', 'installApp.nginx') 8 print(result2) 9 # result2 = saltServer.runRunner('manage.status') 10 # print(result2)
posted on 2018-07-04 23:07 Karlkiller 阅读(232) 评论(0) 编辑 收藏 举报