salt執行模塊
在遠程主機上運行預定義的或任意命令,也稱爲遠程執行,是saltstack的核心功能。
salt執行模塊是由遠程執行系統調用來執行各種各樣的任務。這些模塊提供的功能,如安裝包,重啓服務,遠程命令運行,傳輸文件等。
遠程執行命令:
命令格式
salt 目標 模塊.方法 返回信息
salt '*' test.ping
salt '*' cmd.run "uptime"
salt 'salt-client.com' state.highstate -v test=True #測試
salt 'salt-client.com' state.highstate #主動推送
salt '*' sys.doc #查看哪些函數可用
salt '*' pkg.install vim #安裝包 yum或apt
salt '*' network.interfaces #查看網絡
檢查存活主機
salt "*" test.ping #注意這裏支持通配符哦,*表示所有,當然依舊也可以指定主機,在這裏面test表示模塊而ping則是方法。注意這裏面的ping可不是Linux裏面ICMP協議的ping是salt自己獨有的東西
[root@master ~]# salt "*" test.ping
salt-client.com:
True
命令執行
#查看磁盤利用率
salt "*" cmd.run "df -h"
#其中cmd爲模塊而run是方法df -h則是傳入的參數.此時就能夠返回各個主機的磁盤使用情況。cmd下的run方法後面可以接任何shell參數。因此這個模塊非常之強大,當然也非常危險。
[root@master ~]# salt "*" cmd.run "df -h"
salt-client.com:
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 7.9G 1.6G 6.0G 21% /
tmpfs 242M 12K 242M 1% /dev/shm
/dev/sda1 194M 29M 155M 16% /boot
#查看內存剩餘
salt "*" cmd.run "free -m"
[root@master ~]# salt "*" cmd.run "df -h"
salt-client.com:
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 7.9G 1.6G 6.0G 21% /
tmpfs 242M 12K 242M 1% /dev/shm
/dev/sda1 194M 29M 155M 16% /boot
#遠程創建文件夾
[root@master ~]# salt '*' cmd.run 'mkdir -p /tmp/salt'
salt-client.com:
遠程拷貝文件
我們可以實現將本機的host文件直接複製到客戶端的/tmp目錄下,不必擔心文件權限的問題
[root@master ~]# salt-cp '*' /etc/hosts /tmp
{'salt-client.com': {'/tmp/hosts': True}}
這種方法是最簡單的,不必擔心遠程複製時文件權限的問題,這種方法可比ssh遠程複製要簡單的太多啦
內置的執行模塊
[root@master salt]# salt '*' disk.usage #查看遠程主機磁盤使用情況
salt-client.com:
----------
/:
----------
1K-blocks:
8256952
ervice模塊
salt '*' service.status sshd #查看服務狀態
salt '*' service.restart sshd #重啓sshd服務
salt '*' service.get_all #獲取minion端已啓動的服務
salt-cp模塊
salt-cp '*' /root/salt.txt /root/
file模塊
salt '*' file.file_exists /etc/passwd #檢測文件是否存在
遠程代碼執行
salt '*' cmd.exec_code python 'import sys; print sys.version'
查看執行過程
salt '*' cmd.exec_code python 'import sys; print sys.version' -l debug
saltstack遠程執行幾個典型模塊來學習:
SALT.MODULES.NETWORK
Module for gathering and managing network information
salt.modules.network.active_tcp()
CLI Example:
salt '*' network.active_tcp
salt.modules.network.arp()
Return the arp table from the minion
CLI Example:
salt '*' network.arp
SALT.MODULES.SERVICE
service is a virtual module that is fulfilled by one of the following modules:
salt.modules.service.available(name)
Returns True if the specified service is available, otherwise returns False.
CLI Example:
salt '*' service.available sshd
salt.modules.service.get_all()
Return a list of all available services
CLI Example:
salt '*' service.get_all
salt.modules.service.reload(name)
Refreshes config files by calling service reload. Does not perform a full restart.
CLI Example:
salt '*' service.reload
salt.modules.service.restart(name)
Restart the specified service
CLI Example:
salt '*' service.restart
SALT.MODULES.CP
Minion side functions for salt-cp
salt.modules.cp.cache_dir(path, saltenv='base', include_empty=False, include_pat=None, exclude_pat=None, env=None)
Download and cache everything under a directory from the master
CLI Example:
salt-cp '*’/etc/hots /opt/hehe
SALT.MODULES.STATE
Control the state system on the minion.
CLI Example:
salt '*' state.show_top
saltstack遠程執行-返回程序
SALT.MODULES.MYSQL
Module to provide MySQL compatibility to salt.
說明:返回數據是指minion直接返回給MYSQL
salt.modules.mysql.db_create(name, character_set=None, collate=None, **connection_args)
Adds a databases to the MySQL server.
name
The name of the database to manage
character_set
The character set, if left empty the MySQL default will be used
collate
The collation, if left empty the MySQL default will be used
CLI Example:
salt '*' mysql.db_create 'dbname'
salt '*' mysql.db_create 'dbname' 'utf8' 'utf8_general_ci'
如何編寫一個狀態模塊
1.放在哪裏
/srv/salt/_modules
2.命令。文件名就是模塊名;例如:my_disk.py
vim /_modules/my_disk.py
def list():
cmd = 'df-h'
ret = _salt_['cmd.run'](cmd)
return ret
3.刷新
#刷新命令會把文件放在/etc/salt/minion/extmods裏面
salt '*' saltutil.sync_modules