ansible 核心模塊之 script

script模塊

script 模塊用于在遠程主機上執(zhí)行 ansible 管理主機上的腳本

參數(shù) (=號后面的參數(shù)強制要求):

  • chdir

在執(zhí)行對應(yīng)的命令之前,會先進入到此參數(shù)指定的目錄中
[Default: (null)]
version_added: 0.6

  • creates

當指定的文件存在時,就不執(zhí)行對應(yīng)命令
[Default: (null)]

  • removes

當指定的文件不存在時,就不執(zhí)行對應(yīng)命令
[Default: (null)]
version_added: 0.8

  • = free_form

必須參數(shù),指定需要遠程執(zhí)行的命令,但是并沒有具體的一個參數(shù)名叫 free_form

注意:

  • 最好編寫 ansible 模塊而不是推送腳本
  • windows 目標也支持此模塊
  • 如果本地腳本的路徑包含空格,則需要用引號引起來

區(qū)別:

  • command、shell、raw 和 script 這四個模塊的作用和用法都類似,都用于遠程執(zhí)行命令或腳本:
  • command 模塊:執(zhí)行簡單的遠程 shell 命令,但不支持解析特殊符號 < > | ; & 等,比如需要重定向時不能使用 command 模塊,而應(yīng)該使用shell模塊。但command 模塊更安全,因為他不受用戶環(huán)境的影響, 也很大的避免了潛在的 shell 注入風(fēng)險
  • shell 模塊:和command相同,但是支持解析特殊 shell 符號,但這樣有潛在的 shell 注入風(fēng)險
  • raw 模塊:執(zhí)行底層 shell 命令。command 和 shell 模塊都是通過目標主機上的 python 代碼啟動 /bin/bash 來執(zhí)行命令的,但目標主機上可能沒有安裝 python,這時只能使用 raw 模塊在遠程主機上直接啟動
  • script 模塊:在遠程主機上執(zhí)行腳本文件 ,和 raw 模塊一樣,不要求目標主機上已經(jīng)裝好 python

實例:

# Example from Ansible Playbooks
- script: /some/local/script.sh --some-arguments 1234

# Run a script that creates a file, but only if the file is not yet created
- script: /some/local/create_file.sh --some-arguments 1234
  args:
    creates: /the/created/file.txt

# Run a script that removes a file, but only if the file is not yet removed
- script: /some/local/remove_file.sh --some-arguments 1234
  args:
    removes: /the/removed/file.txt

# Run a script using a executable in a non-system path
- script: /some/local/script
  args:
    executable: /some/remote/executable

# Run a script using a executable in a system path
- script: /some/local/script.py
  args:
    executable: python3
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容