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 注入風險 - shell 模塊:和command相同纱新,但是支持解析特殊 shell 符號展氓,但這樣有潛在的 shell 注入風險
- 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