前面己經說過如何安裝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、多動手。【能打就不要吵】