saltstack編寫自定義模組

2022-08-21 03:33:11 字數 1946 閱讀 4549

salt的底層通訊是通過zeromq完成的,採用了zeromq的訂閱發布模式(pub和sub),如下圖所示。

簡單來講,pub/sub模式類似於廣播電台,在訂閱發布模式中pub將訊息傳送到匯流排,所有的sub收到來自匯流排的訊息後,根據自己的訂閱條件來接收特定的訊息。對應到salt中就是master將事件發布到訊息匯流排,minion訂閱並監聽事件,然後minion會檢視事件是否 ,通過正則匹配的各種方法去匹配minion。下面簡要說明下saltmaster和minion的通訊過程, salt master啟動時會監聽兩個埠,預設是4505和4506。

4506的作用: salt master ret介面,支援認證(auth)、檔案服務、結果收集等功能;   

4505的作用:salt master pub介面,提供遠端執行命令傳送功能。

salt minion啟動時從配置檔案中獲取master的位址,如果為網域名稱,則進行解析。解析完成後,會連線master的4506(ret介面)進行key認證。認證通過,會獲取到master的publish_port(預設是4505),然後連線publish_port訂閱來自master pub介面的任務。當master下發操作指令時,所有的minion都能接收到,然後minion會檢查本機是否匹配。如果匹配,則執行。執行完畢後,把結果傳送到master的4506(ret介面)由master進行處理,命令傳送通訊完全是非同步的,並且命令包很小。此外,這些命令包通過maqpack進行序列化後資料會進一步壓縮(maqpack是一種高效的二進位制序列化格式),所以salt的網路負載非常低。

編寫乙個自定義的測試模組 hello.py。預設情況下,我們的定製化模組存放於/srv/salt/_modules/資料夾下,這個資料夾並不是預設建立的,需要手動建立。

[root@29-server ~]# mkdir -pv /srv/salt/_modules/

[root@29-server ~]# cd /srv/salt/_modules/

[root@29-server _modules]# cat hello.py

def world():

"""it is my first function.

example:

salt '*' hello.world

"""return "hello world!"

模組新增完畢後,我們需要把模組推送到所有minion上。

[root@29-server _modules]# salt "*" saltutil.sync_modules

minion-one:

- modules.hello

返回結果是minion接收到的新模組名。由於每個minion都接收了我們的新模組,所以全部返回了- modules.hello。

[root@29-server _modules]# salt "*" hello.world

minion-one:

hello world!

檢視模組幫助資訊

[root@29-server _modules]# salt "minion-one" sys.doc hello

hello.world:

it is my first function.

example:

salt '*' hello.world

1.__salt__2.__grains__3.__pillar__4.__virtual__

python from import 自定義模組

from douban250.items import douban250item python import 自定義模組 1 主程式與模組程式在同一目錄下 如下面程式結構 src mod1.py test1.py 若在程式test1.py中匯入模組mod1,則直接使用 import mod1或fr...

編寫自定義特性

自定義特性一般標註在作用的程式元素的上方 編譯器首先組合特性名稱和attribute,然後尋找該組合名,所以特性名為fieldname和fieldnameattribute的兩個特性沒有區別 編譯器將尋找包含有這個名稱的類,它直接或者間接的派生資system.attribute 編寫自定義類的步驟 ...

編寫自定義GenericServlet

具體步驟如下 1 新建類mygenericservlet,實現servlet,servletconfig介面 2 重寫父類的相關方法 3 將service 方法定義為抽象 abstract 方法,將類定義為抽象類,以便在子類予以service 方法新的實現 4 定義mygenericservlet的...