簡介
雖然shell模塊看起來非常萬能,但存在着許多風險。例如:shell注入風險,非冪等性(即統一命令多次執行的結果可能是不一樣的)
如何使用
幫助
ansible-doc -s shell - name: Execute commands in nodes. action: shell chdir # cd into this directory before running the command creates # a filename, when it already exists, this step will *not* be run. executable # change the shell used to execute the command. Should be an absolute path to the executable. free_form= # The shell module takes a free form command to run, as a string. There's not an actual option named "free form". See the examples! removes # a filename, when it does not exist, this step will *not* be run. warn # if command warnings are on in ansible.cfg, do not warn about this particular line if set to no/false.
說明
chdir:在哪個目錄下執行shell命令,相當於執行shell命令前先cd到對應目錄
creates:如果存在某文件,就不執行shell
removes:如果不存在某文件,就不執行shell;與creates左右相反(有點繞)
executable:修改並指定shell解釋器來執行命
free_form:指的就是具體的shell命令,實際上是一個不存在的選項
例子
/etc/ansible/hosts配置如下:
[local] 127.0.0.1
1) 查看當前目錄,在local節點上運行
#-m shell表示使用shell模塊 #-a 表示傳遞shell模塊的相關參數 #-i 指定了inventory文件 #local是inventory文件里面定義好的節點 ansible -i /etc/ansible/hosts local -m shell -a 'pwd'
2) 設定/root/ccg為當前目錄,在local節點上運行

也可以寫成:ansible -i /etc/ansible/hosts local -m shell -a 'cd /root/ccg && pwd'
3) 使用shell解釋器為dash執行
ansible -i /etc/ansible/hosts local -m shell -a 'pwd executable=/bin/dash'
4) 如果存在~/.ssh/id_rsa則不執行該shell命令
ansible -i /etc/ansible/hosts local -m shell -a 'pwd creates=~/.ssh/id_rsa'

5)如果存在~/.ssh/id_rsa則執行該shell命令
ansible -i /etc/ansible/hosts local -m shell -a 'pwd remove=~/haha'
以上就是ansible的shell模塊的解釋,后面會帶來command,raw,script模塊的使用分享。
博主:測試生財(一個不為996而996的測開碼農)
座右銘:專注測試開發與自動化運維,努力讀書思考寫作,為內卷的人生奠定財務自由。
內容范疇:技術提升,職場雜談,事業發展,閱讀寫作,投資理財,健康人生。
csdn:https://blog.csdn.net/ccgshigao
博客園:https://www.cnblogs.com/qa-freeroad/
51cto:https://blog.51cto.com/14900374
微信公眾號:測試生財(定期分享獨家內容和資源)