标签:cp cron salt saltstack runner
前面己经说过如何安装saltstack,在平时己经体验了它的强大,下面来说一下平时常用的cp模块及cron与sls文件的书写
一、salt.modules.cp
1、复制文件[我使用最多的]
格式:
salt.modules.cp.get_file(path, dest, saltenv=‘base‘, makedirs=False, template=None, gzip=None, env=None,**kwargs)
常用参数说明:
path:表示salt-master的file_roots的路径下的源文件
dest:表示salt-minion的路径下文件(可以指定文件名,也可以不指定,但不指定【有时】会报错)
saltenv:salt-master部署到的环境,可以/etc/salt/master文件的file_roots自行指定,默认为base
makedirs:类型为布尔型,True表示目录不存在创建,False表示不创建
template: 指定模板,常用的有jinja。。。
gzip:压缩级别
例子:
salt ‘192.168.2.36-CentOS.backup.test.backend‘ cp.get_file salt://crontab/bak_sys.py /root/bak_sys.py makedirs=True gzip=9
2、复制目录
格式:
salt.modules.cp.get_dir(path, dest, saltenv=‘base‘, template=None, gzip=None, env=None, **kwargs)
注:path在此处表示目录,如果你不想将path复制到dest之下,那么指定到dest的上一级就好
例子:
#salt ‘test-201‘ cp.get_dir salt://crontab /root makedirs=True test-201: - /root/crontab/Insert_Outgoing_Mail_Total.py - /root/crontab/Redis_config.py .....
3、查看文件内容
格式:
salt.modules.cp.get_file_str(path, saltenv=‘base‘, env=None)
例子:
[root@report copy_crontab_file]# salt ‘test-201‘ cp.get_file_str salt://crontab/delete_mysql_rsyslog.sh test-201: #!/bin/bash # Database maintance script which can be used for rsyslog # and phplogcon default database schema. # Michael Mansour suggested it to be included - thx! # This program was original part of of PHPloghost # Copyright (C) 2004 Tuatha de Dana # some modifications for rsyslog by mmeckelein at 2007-08-08 # 2007-08-13 mmeckelein: added dbhost and some other improvements # suggested by Michael Mansour - thx a lot! .....
二、salt.modules.cron
1、添加计划任务
格式:
salt.modules.cron.set_job(user, minute, hour, daymonth, month, dayweek, cmd, comment=None,identifier=None)
参数说明:
user:用户;剩下的表示分、时、日、月、周、命令、任务描述信息
例子:
salt \* cron.set_job root ‘0‘ ‘03‘ ‘*‘ ‘*‘ ‘*‘ ‘/bin/bash /root/crontab/rsync_mysql_backup.sh‘ ‘remote backup mysql data to xxxxxxx/backup/local_mysql_backup/ by king.gp‘
说明:每一个参数项都要用引号括起来。
2、删除计划任务
格式:
salt.modules.cron.rm_job(user, cmd, minute=None, hour=None, daymonth=None, month=None,dayweek=None, identifier=None)
例子:
salt ‘*‘ -b 3 cron.rm_job root ‘/root/crontab/rsync_mysql_backup.sh‘
三、添加计划任务states文件版本。
注:
要在salt-master端设置file_roots (略)
在file_roots指定的目录下创建top.sls文件
定义init.sls文件
1、我们第二步开始,file_roots设置top文件
[root@report /]# egrep -v "^$|^#" /etc/salt/master file_roots: base: - /home/sysadmin/yunwei - /home/sysadmin/yunwei/workspace
2、创建top.sls文件
[root@report workspace]# pwd /home/sysadmin/yunwei/workspace [root@report workspace]# cat top.sls base: "test-201": - install_python_ext - add_crontab_file
注:如果file_roots指向多个路径,salt则取它们的合集
3、定义init.sls文件
注:也是可以直接在top.sls的同级目录写sls文件的,但这样到后期要维护太多的文件,而且看上会会很乱,如果定义多级目录,就可在同名目录中写【init.sls】文件了
[root@report workspace]# cd add_crontab_file/ [root@report add_crontab_file]# pwd /home/sysadmin/yunwei/workspace/add_crontab_file [root@report add_crontab_file]# cat init.sls /root/crontab: file.directory: - user: root - group: root - mode: 755 - makedirs: True /root/crontab/clean_mail_local_mysql_history_data.sh: cron.present: - user: root - minute: 30 - hour: 23 - daymonth: 28 - comment: ‘clean local mysql data info by king.gp‘ /root/crontab/Insert_Outgoing_Mail_Total.py: cron.present: - user: root - minute: 0 - hour: 23 - comment: ‘mail outgoing total by king.gp‘ /root/crontab/rsync_mysql_backup.sh: cron.present: - user: root - minute: 0 - hour: 3 - comment: ‘remote backup mysql data to 192.168.3.34/backup/local_mysql_backup/ by king.gp‘ /root/crontab/rsync_redis_backup.sh: cron.present: - user: root - minute: 0 - hour: ‘*/2‘ - comment: ‘remote backup redis data to 192.168.3.37/archive/redis_backup/ by king.gp‘ /root/crontab/delete_mysql_rsyslog.sh: cron.present: - user: root - minute: 0 - hour: 1 - comment: ‘del local mysql syslog data‘
注:由于有一个脚本用python写的,还要安装一些python的扩展包
[root@report copy_crontab_file]# cd .. [root@report workspace]# cd install_python_ext/ [root@report install_python_ext]# pwd /home/sysadmin/yunwei/workspace/install_python_ext [root@report install_python_ext]# cat init.sls python_ext: pkg.installed: - pkgs: - python-setuptools - python-pip - MySQL-python
4、定义runner文件
[root@report workspace]# pwd /home/sysadmin/yunwei/workspace [root@report workspace]# cat cron_runner.sls python_ext: salt.state: - tgt: ‘test-201‘ - highstate: True corn_exec: salt.state: - tgt: ‘test-201‘ - highstate: True - require: - salt: python_ext
5、测试runner文件
[root@report workspace]# salt-run state.orchestrate cron_runner test=Ture report.chinadns.net_master: ---------- ID: python_ext Function: salt.state Result: True Comment: States ran successfully. No changes made to test-201. Started: 18:00:13.372399 Duration: 8079.834 ms Changes: ---------- ID: corn_exec Function: salt.state Result: True Comment: States ran successfully. No changes made to test-201. Started: 18:00:21.452916 Duration: 8101.168 ms Changes: Summary ------------ Succeeded: 2 Failed: 0 ------------ Total states run: 2
注:此处我是先将计划任务的目录拷贝到test-201上
salt ‘test-201‘ cp.get_file salt://crontab /root makedirs=True
6、执行定义好的runner文件
[root@report workspace]# salt-run state.orchestrate cron_runner report.chinadns.net_master: ---------- ID: python_ext Function: salt.state Result: True Comment: States ran successfully. No changes made to test-201. Started: 18:03:39.272417 Duration: 8305.688 ms Changes: ---------- ID: corn_exec Function: salt.state Result: True Comment: States ran successfully. No changes made to test-201. Started: 18:03:47.578834 Duration: 8102.33 ms Changes: Summary ------------ Succeeded: 2 Failed: 0 ------------ Total states run: 2
7、查看结果
[root@report workspace]# salt ‘test-201‘ cron.ls root test-201: ---------- crons: |_ ---------- cmd: /root/crontab/clean_mail_local_mysql_history_data.sh comment: clean local mysql data info by king.gp daymonth: 28 dayweek: * hour: 23 identifier: /root/crontab/clean_mail_local_mysql_history_data.sh minute: 30 month: * |_ ---------- cmd: /root/crontab/Insert_Outgoing_Mail_Total.py comment: mail outgoing total by king.gp daymonth: * dayweek: * hour: 23 identifier: /root/crontab/Insert_Outgoing_Mail_Total.py minute: 0 month: * |_ ---------- cmd: /root/crontab/rsync_mysql_backup.sh comment: remote backup mysql data to 192.168.3.37/backup/local_mysql_backup/ by king.gp daymonth: * dayweek: * hour: 3 identifier: /root/crontab/rsync_mysql_backup.sh minute: 0 month: * |_ ---------- cmd: /root/crontab/rsync_redis_backup.sh comment: remote backup redis data to 192.168.3.34/archive/redis_backup/ by king.gp daymonth: * dayweek: * hour: */2 identifier: /root/crontab/rsync_redis_backup.sh minute: 0 month: * |_ ---------- cmd: /root/crontab/delete_mysql_rsyslog.sh comment: del local mysql syslog data daymonth: * dayweek: * hour: 1 identifier: /root/crontab/delete_mysql_rsyslog.sh minute: 0 month: * env: pre: special:
注:最通用的还是写piller,但现在我还是用不明白,就不丢人了!
四、总结
1、多看文档,多总结;【E文没过级,but我有梯子】
2、如果别人发现你用方法很蠢,请接受;
3、多动手。【能打就不要吵】
本文出自 “和风细雨” 博客,请务必保留此出处http://essun.blog.51cto.com/721033/1705840
标签:cp cron salt saltstack runner
原文地址:http://essun.blog.51cto.com/721033/1705840