rwa模塊
raw 模塊用于在遠(yuǎn)程主機(jī)上執(zhí)行 ansible 管理主機(jī)上的腳本
參數(shù) (=號(hào)后面的參數(shù)強(qiáng)制要求):
- = free_form
必須參數(shù)搅方,指定需要遠(yuǎn)程執(zhí)行的命令,但是并沒有具體的一個(gè)參數(shù)名叫
free_form
- executable
通過使用絕對路徑修改并指定 shell 解釋器來執(zhí)行命令
當(dāng)使用特權(quán)升級become
時(shí)黍析,如果沒有提供 shell,因?yàn)樘貦?quán)升級需要一個(gè) shell屎开,所以默認(rèn)外殼將被分配
[Default: (null)]
version_added: 1.0
注意:
- 如果要安全且可預(yù)測地執(zhí)行命令阐枣,最好改用 command 或 shell 模塊
- windows 目標(biāo)也支持此模塊
區(qū)別:
- command、shell奄抽、raw 和 script 這四個(gè)模塊的作用和用法都類似蔼两,都用于遠(yuǎn)程執(zhí)行命令或腳本:
- command 模塊:執(zhí)行簡單的遠(yuǎn)程 shell 命令,但不支持解析特殊符號(hào)
< > | ; &
等如孝,比如需要重定向時(shí)不能使用 command 模塊宪哩,而應(yīng)該使用shell模塊。但command 模塊更安全第晰,因?yàn)樗皇苡脩舡h(huán)境的影響锁孟, 也很大的避免了潛在的 shell 注入風(fēng)險(xiǎn) - shell 模塊:和command相同,但是支持解析特殊 shell 符號(hào)茁瘦,但這樣有潛在的 shell 注入風(fēng)險(xiǎn)
- raw 模塊:執(zhí)行底層 shell 命令品抽。command 和 shell 模塊都是通過目標(biāo)主機(jī)上的 python 代碼啟動(dòng)
/bin/bash
來執(zhí)行命令的,但目標(biāo)主機(jī)上可能沒有安裝 python甜熔,這時(shí)只能使用 raw 模塊在遠(yuǎn)程主機(jī)上直接啟動(dòng) - script 模塊:在遠(yuǎn)程主機(jī)上執(zhí)行腳本文件 圆恤,和 raw 模塊一樣,不要求目標(biāo)主機(jī)上已經(jīng)裝好 python
實(shí)例:
- name: Bootstrap a legacy python 2.4 host
raw: yum -y install python-simplejson
- name: Bootstrap a host without python2 installed
raw: dnf install -y python2 python2-dnf libselinux-python
- name: Run a command that uses non-posix shell-isms (in this example /bin/sh doesn't handle redirection and wildcards together but bash does)
raw: cat < /tmp/*txt
args:
executable: /bin/bash
- name: safely use templated variables. Always use quote filter to avoid injection issues.
raw: "{{package_mgr|quote}} {{pkg_flags|quote}} install {{python_simplejson|quote}}"