4.1 問題
本案例要求:
shell
執行以下命令查看結果,並說明原因
ansible web -m shell -a "echo ${HOSTNAME}"
ansible web -m shell -a 'echo ${HOSTNAME}'
testfile 文件在哪裏
ansible cache -m shell -a 'cd /tmp'
ansible cache -m shell -a 'touch testfile'
4.2 步驟
實現此案例需要按照如下步驟進行。
1)shell
ansible web -m shell -a "echo ${HOSTNAME}"
ansible web -m shell -a 'echo ${HOSTNAME}'
2)創建的文件在哪查看
ansible cache -m shell -a 'cd /tmp'
ansible cache -m shell -a 'touch testfile'
注:
1)變量解析
ansible 執行命令是二次解析,第一次在本機解析, 第二次在執行機器解析,需要第二次解析的變量要轉移(\)
2)創建的文件在哪裏
文件在用戶家目錄,ansible 是使用 ssh 多次連接執行,連接退出以後之前的狀態就全部失效了
解決方法:使用 chdir 代替 cd 命令
ansible cache -m shell -a ‘chdir=/tmp touch testfile’