> 1. command模塊,用來在遠程主機上執行命令
> 2. 使用 command 模塊在遠程主機中執行命令時,不會經過遠程主機的 shell 處理,在使用 command 模塊時,如果需要執行的命令中含有重定向、管道符等操作時,這些符號也會失效,比如”<”, “>”, “|”, “;” 和 “&” 這些符號,如果你需要這些功能,可以參考后面介紹的 shell 模塊。
> 3. 如果遠程節點是 windows 操作系統,則需要使用 win_command 模塊。
> 執行 ansible 時,不加 -m 默認使用 command ,可以在 /etc/ansible/ansible.cfg 中修改。
```
# default module name for /usr/bin/ansible
#module_name = command
```
## 1. 選項
### free_form參數 :
> 必須參數,指定需要遠程執行的命令。需要說明一點,free_form 參數與其他參數(如果想要使用一個參數,那么則需要為這個參數賦值,也就是name=value模式)并不相同。比如,當我們想要在遠程主機上執行 ls 命令時,我們并不需要寫成”free_form=ls” ,這樣寫反而是錯誤的,因為并沒有任何參數的名字是 free_form,當我們想要在遠程主機中執行 ls 命令時,直接寫成 ls 即可。因為 command 模塊的作用是執行命令,所以,任何一個可以在遠程主機上執行的命令都可以被稱為 free_form。
### chdir參數 :
> 此參數的作用就是指定一個目錄,在遠程主機執行對應的命令之前,會先進入到 chdir 參數指定的目錄中。
### creates參數 :
> 看到 creates,你可能會從字面上理解這個參數,但是使用這個參數并不會幫助我們創建文件,它的作用是當指定的文件存在時,就不執行對應命令,比如,如果 /testdir/test文件存在,就不執行我們指定的命令。
### removes參數 :
> 與 creates 參數的作用正好相反,它的作用是當指定的文件不存在時,就不執行對應命令,比如,如果 /testdir/tests 文件不存在,就不執行我們指定的命令,此參數并不會幫助我們刪除文件。
```
[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "ls"
ansible-demo3 | SUCCESS | rc=0 >>
anaconda-ks.cfg
CentOS7-Base-163.repo
Centos-7.repo
```
> 上面命令表示在 ansible-demo3 主機上執行 ls 命令,因為使用的是 root 用戶,所以默認情況下,ls 出的結果是 ansible-demo3 主機中 root 用戶家目錄中的文件列表。
>
```
[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "chdir=/testdir ls"
ansible-demo3 | SUCCESS | rc=0 >>
testfile1
testfile2
```
> chdir 參數表示執行命令之前,會先進入到指定的目錄中,所以上面命令表示查看 ansible-demo3 主機上 /testdir 目錄中的文件列表,返回顯示有2個文件。
```
[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "creates=/testdir/testfile1 echo test"
ansible-demo3 | SUCCESS | rc=0 >>
skipped, since /testdir/testfile1 exists
[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "creates=/testdir/testfile3 echo test"
ansible-demo3 | SUCCESS | rc=0 >>
test
```
上面命令表示 /testdir/testfile1 文件存在于遠程主機中,則不執行對應命令。/testdir/testfile3 不存在,才執行”echo test”命令。
```
[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "removes=/testdir/testfile1 echo test"
ansible-demo3 | SUCCESS | rc=0 >>
test
[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "removes=/testdir/testfile3 echo test"
ansible-demo3 | SUCCESS | rc=0 >>
skipped, since /testdir/testfile3 does not exist
```
- Linux
- 高級
- 殺毒
- 記一次中毒事件
- clamav查毒軟件
- 處理挖礦病毒
- 定時任務
- kill
- chattr文件保護
- 運行級別
- Linux啟動
- 文件加密
- ssh免密登錄
- .ssh
- 問題
- 腳本
- 阿里云域名解析
- yum源
- 時間同步
- keepalived實現高可用
- dos字符與unix字符
- 大文件上傳
- 基礎
- proc目錄
- 設置宋體
- 基礎命令_01
- 基礎命令_02
- SELinux
- 文件描述符
- 基礎命令_03
- awk
- 系統日志
- date命令
- bc命令
- lsof
- vim快捷鍵
- shell
- 循環控制
- expr
- 執行腳本的方式
- declare
- shell腳本
- 控制啟停腳本
- 數值計算
- centos
- 配置網絡
- 環境
- 灰度環境
- ansible
- 模塊
- 語法
- file模塊
- setup模塊
- ping模塊
- copy模塊
- command模塊
- shell模塊
- service模塊
- cron模塊
- yum模塊
- user 模塊
- group模塊
- 指定用戶
- playbook
- 實例
- ansible安裝
- Jenkins
- shell部署
- 導入已有項目的配置
- 執行shell
- tungsten數據同步
- 防火墻
- netfilter