實(shí)戰(zhàn)指南:從 CLI 參數(shù)到源碼級(jí)縮進(jìn)與解析原理)
運(yùn)維配置管理后端【免費(fèi)下載鏈接】saltSoftware to automate the management and configuration of infrastructure and applications at scale.項(xiàng)目地址https://gitcode.com/gh_mirrors/sa/salt點(diǎn)擊查看免費(fèi)下載導(dǎo)讀Salt 的輸出器outputter負(fù)責(zé)把 minion 返回的數(shù)據(jù)格式化成人類可讀或機(jī)器可解析的文本。其中json_out模塊將返回?cái)?shù)據(jù)序列化為標(biāo)準(zhǔn) JSON是腳本化、自動(dòng)化對(duì)接 Salt 時(shí)最常用的輸出器。本文以 salt.output.json_out.rst 為骨架結(jié)合 salt/output/json_out.py 源碼、tests/pytests/unit/output/test_json_out.py 與 tests/pytests/integration/cli/test_salt_call.py 集成測(cè)試帶你掌握--outjson的完整用法、--out-indent與output_indent的取值語義以及輸出器在 Salt 內(nèi)部的調(diào)度與序列化細(xì)節(jié)。一、模塊定位Salt 的 JSON 輸出器Salt 內(nèi)置多種輸出器highstate、json、key、nested、pprint、raw、txt、yaml等完整清單見 doc/ref/output/all/index.rst。json_out模塊的虛擬名virtual name是json通過__virtual__()將模塊重命名為json因此在命令行中寫--outjson即可觸發(fā)它見 salt/output/json_out.py。json_out的核心價(jià)值在于輸出是標(biāo)準(zhǔn) JSON任何語言的 JSON 解析器都能直接消費(fèi)特別適合需要把 Salt 執(zhí)行結(jié)果喂給 CI/CD、監(jiān)控告警、自研 Web 平臺(tái)的場(chǎng)景。二、觸發(fā)方式命令行與配置雙入口2.1 命令行指定所有 Salt CLIsalt、salt-call、salt-run、salt-key等都支持通用輸出選項(xiàng)定義見 doc/ref/cli/_includes/output-options.rstsalt * test.ping --outjson salt * network.hw_addr en0 --outjson--out的取值見 salt/utils/parsers.py它映射到destoutput。2.2 配置文件中固化如果不希望每次敲命令都帶參數(shù)可以在 Master 或 Minion 配置文件中設(shè)置output和output_indent例如conf/master、conf/minion。配置后所有輸出默認(rèn)走 JSON。三、縮進(jìn)控制--out-indent 與 output_indent這是json_out文檔中最核心的配置話題官方文檔明確給出三種取值語義見 salt/output/json_out.py取值語義NullNone每個(gè) minion 的返回結(jié)果合并為單行JSONpretty使用四空格縮進(jìn)且對(duì)鍵排序sort_keys整數(shù)指定縮進(jìn)級(jí)別空格數(shù)負(fù)數(shù)等價(jià)于單行3.1 通過 CLI 標(biāo)志設(shè)置# 單行緊湊輸出每 minion 一行適合逐行解析 salt * test.ping --outjson --out-indent-1 # 按空格數(shù)縮進(jìn) salt * test.ping --outjson --out-indent2 # 鍵排序 四空格縮進(jìn)pretty salt-call test.ping --outjson --output-indentpretty--out-indent與--output-indent是同一選項(xiàng)的兩個(gè)別名在 salt/utils/parsers.py 中聲明destoutput_indent、typeint。注意CLI 層面它是整數(shù)類型所以pretty這種字符串取值只能通過配置文件output_indent設(shè)置。3.2 配置文件方式在conf/master或conf/minion中設(shè)置output: json output_indent: pretty # 或 None或整數(shù)四、按 minion 逐條輸出的設(shè)計(jì)Salt 的輸出器是按 minion 粒度工作的每個(gè) minion 的返回?cái)?shù)據(jù)到達(dá) Master 后會(huì)被各自序列化為一個(gè)獨(dú)立的 JSON 對(duì)象見 salt/output/json_out.py。$ salt * network.hw_addr en0 --outjson --out-indent-1 {dave: {en0: {hwaddr: 02:b0:26:32:4c:69, ...}}} {jerry: {en0: {hwaddr: 02:26:ab:0d:b9:0d, ...}}} {kevin: {en0: {hwaddr: 02:6d:7f:ce:9f:ee, ...}}} {mike: {en0: {hwaddr: 02:48:a2:4b:70:a0, ...}}} {phill: {en0: {hwaddr: 02:1d:cc:a2:33:55, ...}}} {stuart: {en0: {hwaddr: 02:9a:e0:ea:9e:3c, ...}}}官方文檔特別提醒見 salt/output/json_out.py部分 JSON 解析器能猜出對(duì)象邊界但很多不能??煽康淖龇ㄊ鞘褂脝涡休敵龈袷街鹦薪馕觥啃幸粋€(gè)完整 JSON 對(duì)象天然避免跨行截?cái)鄦栴}。例如配合jqsalt * test.ping --outjson --out-indent-1 | jq -r keys[]五、源碼級(jí)實(shí)現(xiàn)剖析5.1 output() 的縮進(jìn)決策邏輯json_out.output(data, **kwargs)是模塊入口見 salt/output/json_out.py其完整決策樹如下若__opts__中不存在output_indent鍵直接以indent4輸出默認(rèn)行為等價(jià)于四空格縮進(jìn)但不排序若output_indent為NoneindentNone輸出為單行緊湊 JSON若為prettyindent4且sort_keysTrue鍵按字典序排序若為整數(shù)直接作為縮進(jìn)空格數(shù)負(fù)數(shù)被歸一化為None單行。最終統(tǒng)一調(diào)用salt.utils.json.dumps(data, defaultrepr, indentindent, sort_keyssort_keys)其中defaultrepr非常關(guān)鍵當(dāng)返回?cái)?shù)據(jù)里含有 JSON 無法原生序列化的對(duì)象如 Python 的datetime、自定義類時(shí)會(huì)退而求其次用repr()的字符串形式代替保證整體仍是合法 JSON。5.2 異常兜底except UnicodeDecodeError as exc: log.error(Unable to serialize output to json) return salt.utils.json.dumps({error: Unable to serialize output to json, message: str(exc)}) except TypeError: log.debug(An error occurred while outputting JSON, exc_infoTrue) return salt.utils.json.dumps({})遇到不可解碼的字節(jié)序列時(shí)返回一個(gè)帶error/message字段的 JSON 錯(cuò)誤對(duì)象方便調(diào)用方程序化感知失敗遇到其他序列化TypeError時(shí)返回{}保證任何情況下輸出的都是合法 JSON不會(huì)向管道吐出裸異常堆棧。5.3 底層序列化封裝 salt.utils.jsonjson_out使用的 salt/utils/json.py 是對(duì)標(biāo)準(zhǔn)庫json的封裝幾個(gè)值得注意的點(diǎn)Unicode 兼容dumps/dump默認(rèn)ensure_asciiFalse中文等非 ASCII 字符原樣輸出而非轉(zhuǎn)義成\uXXXX便于閱讀與傳輸見 salt/utils/json.py。這解釋了單元測(cè)試test_unicode_output中Д能原樣出現(xiàn)在輸出里見 tests/pytests/unit/output/test_json_out.py快速庫優(yōu)先import_json()按ujson → yajl → json的順序嘗試導(dǎo)入優(yōu)先使用更快的 C 實(shí)現(xiàn)見 salt/utils/json.pybytes 兼容loads遇到 Python 3.6 無法直接解析的 bytestring 時(shí)自動(dòng)轉(zhuǎn)成 Unicode 再解析見 salt/utils/json.py。六、輸出器調(diào)度與回退機(jī)制當(dāng)用戶指定--outjson后數(shù)據(jù)流向?yàn)橐?salt/output/init.pydisplay_output()先調(diào)用try_printout()取格式化結(jié)果try_printout()通過salt.loader.outputters(opts)加載所有輸出器若指定的輸出器不存在則回退到nested再不行回退到raw見 salt/output/init.py若指定了--out-file/--output-file結(jié)果寫入文件追加模式而非 stdout否則通過print_cli()打印。因此即使json_out模塊異常Salt 仍會(huì)兜底輸出而不是靜默丟棄數(shù)據(jù)。七、測(cè)試驗(yàn)證行為即契約json_out的行為被單元測(cè)試與集成測(cè)試雙重鎖定可作為你驗(yàn)證本地 Salt 環(huán)境輸出行為的參照單元測(cè)試 tests/pytests/unit/output/test_json_out.py 覆蓋無output_indent時(shí)默認(rèn)輸出、pretty鍵排序、整數(shù)縮進(jìn)、0/負(fù)數(shù)縮進(jìn)、Unicode 輸出集成測(cè)試 tests/pytests/integration/cli/test_salt_call.py 用真實(shí) CLI 斷言了三種縮進(jìn)下的精確輸出# --out-indent-1 {local: true} # --out-indent0 { local: true } # --out-indent1 { local: true }這些斷言與源碼中負(fù)數(shù) → 單行、整數(shù) → 空格數(shù)的規(guī)則完全一致是理解行為最可靠的參考。八、實(shí)戰(zhàn)建議腳本解析首選單行--outjson --out-indent-1保證每個(gè) minion 一條記錄逐行json.loads()即可避免依賴解析器的對(duì)象邊界猜測(cè)能力日志/審計(jì)用 pretty需要人讀時(shí)用--output-indentpretty四空格 鍵排序輸出確定性強(qiáng)便于 diff程序化錯(cuò)誤感知序列化失敗時(shí)輸出的是帶error字段的合法 JSON調(diào)用方應(yīng)顯式檢查該字段結(jié)果落盤配合--out-file將 JSON 結(jié)果直接追加寫入文件供下游批處理消費(fèi)。從 CLI 標(biāo)志到output_indent配置從單行逐條解析到repr兜底與 Unicode 原樣輸出json_out是理解 Salt 輸出器體系模塊加載、虛擬名、回退機(jī)制的最佳切入口如需查看其他輸出器可繼續(xù)閱讀 doc/ref/output/all/index.rst 中的yaml_out、nested等模塊文檔。贊分享運(yùn)維配置管理后端【免費(fèi)下載鏈接】saltSoftware to automate the management and configuration of infrastructure and applications at scale.項(xiàng)目地址https://gitcode.com/gh_mirrors/sa/salt點(diǎn)擊查看免費(fèi)下載相關(guān)推薦RemoveWindowsAI 命令行參數(shù)速查3 種組合搞定 90% 的清理場(chǎng)景RemoveWindowsAI 命令行參數(shù)速查3 種組合搞定 90% 的清理場(chǎng)景 RemoveWindowsAI 是一個(gè)用于移除 Windows 11 中 C運(yùn)維配置管理后端Salt Master 配置完全指南從 /etc/salt/master 到源碼級(jí)參數(shù)解析Salt Master 配置完全指南從 /etc/salt/master 到源碼級(jí)參數(shù)解析 SaltSaltStack是一個(gè)用于大規(guī)?;A(chǔ)設(shè)施與應(yīng)用配置管運(yùn)維配置管理后端Velero CLI 完全解析get restores 命令的參數(shù)、輸出格式與源碼級(jí)實(shí)現(xiàn)原理Velero CLI 完全解析 get restores 命令的參數(shù)、輸出格式與源碼級(jí)實(shí)現(xiàn)原理 本文以 Velero前身 Ark早期版本 v0.5.0 的云原生災(zāi)備存儲(chǔ)后端上一篇把 Webamp 變成 Spotify 皮膚__customMediaClass 自定義媒體播放終極指南下一篇3分鐘搞定Axure RP中文界面告別英文煩惱的終極指南創(chuàng)作聲明:本文部分內(nèi)容由AI輔助生成(AIGC),僅供參考