標籤:切換目錄 not 簡介 表達 強制 其他 ext pre ansible模組
本文主要介紹Ansible的幾個命令模組,包括:
command - 在遠程節點上執行命令
shell - 讓遠程主機在shell進程下執行命令
script - 將本地script傳送到遠程主機之後執行
raw - 執行低級的和髒的SSH命令
expect - 執行命令並響應提示
telnet - 執行低級的和髒的telnet命令
command模組簡介
command模組用於在給的的節點上運行系統命令,比如echo hello。
- 它不會通過shell處理命令,因此不支援像
$HOME這樣的變數和,以及<, >, |, ;和&等都是無效的。也就是在command模組中無法使用管道符。
模組參數
| 名稱 |
必選 |
備忘 |
| chdir |
no |
運行command命令前先cd到這個目錄 |
| creates |
no |
如果這個參數對應的檔案存在,就不運行command |
| free_form |
yes |
需要執行的指令碼(沒有真正的參數為free_form) |
| executable |
no |
改變用來執行命令的shell,應該是可執行檔的絕對路徑。 |
| removes |
no |
如果這個參數對應的檔案不存在,就不運行command,與creates參數的作用相反 |
| stdin(2.4後新增) |
no |
將命令的stdin設定為指定的值 |
樣本
[[email protected] ~]# ansible test -m command -a "ls /root"172.20.21.120 | SUCCESS | rc=0 >>anaconda-ks.cfgtest.shwhoami.rst[[email protected] ~]# ansible test -m command -a "ls /root creates=test.sh"172.20.21.120 | SUCCESS | rc=0 >>skipped, since test.sh exists[[email protected] ~]# ansible test -m command -a "ls /root removes=test.sh1"172.20.21.120 | SUCCESS | rc=0 >>skipped, since test.sh1 does not exist
在這個裡面,首先更換目錄到root目錄中,然後查看test.sh是否存在,如果存在,那麼命令不會執行;如果不存在,那麼執行命令。
在這裡也可以看到,命令是必須存在的,但是沒有參數名為free_form參數。
[[email protected] ~]# ansible test -m command -a "cat test.sh chdir=/root"172.20.21.120 | SUCCESS | rc=0 >>#!/bin/bashi=0echo $((i+1))[[email protected] ~]# ansible test -m command -a "sh test.sh chdir=/root"172.20.21.120 | SUCCESS | rc=0 >>1
[[email protected] ~]# ansible test -m command -a "ls /root | grep test"172.20.21.120 | FAILED | rc=2 >>/root:anaconda-ks.cfgtest.shwhoami.rstls: 無法訪問|: 沒有那個檔案或目錄ls: 無法訪問grep: 沒有那個檔案或目錄ls: 無法訪問test: 沒有那個檔案或目錄non-zero return code
注意事項
- 若要通過shell運行一個命令,比如
<, >, |等,你實際上需要shell模組。
command模組更安全,因為它不受使用者環境的影響
- 從版本2.4開始,
executable參數被刪除。如果您需要此參數,請改用shell模組。
- 對於Windows節點,請改用
win_command模組。
shell模組簡介
讓遠程主機在shell進程下執行命令,從而支援shell的特性,如管道等。與command模組幾乎相同,但在執行命令的時候使用的是/bin/sh。
模組參數
| 名稱 |
必選 |
備忘 |
| chdir |
no |
運行command命令前先cd到這個目錄 |
| creates |
no |
如果這個參數對應的檔案存在,就不運行command |
| executable |
no |
改變用來執行命令的shell,應該是可執行檔的絕對路徑。 |
| free_form |
yes |
需要執行的指令碼(沒有真正的參數為free_form) |
| removes |
no |
如果這個參數對應的檔案不存在,就不運行command,與creates參數的作用相反 |
| stdin(2.4後新增) |
no |
將命令的stdin設定為指定的值 |
樣本
[[email protected] ~]# ansible test -m shell -a "sh test.sh > result chdir=/root"172.20.21.120 | SUCCESS | rc=0 >>[[email protected] ~]# ansible test -m shell -a "cat result chdir=/root"172.20.21.120 | SUCCESS | rc=0 >>1
注意事項
- 如果你想安全可靠的執行命令,請使用
command模組,這也是編寫playbook的最佳實務。
script模組簡介
script模組的作用是將本地script傳送到遠程主機之後執行
- 給定的指令碼將通過遠程節點上的shell環境進行處理
script模組在遠程系統上不需要python的支援
模組參數
| 名稱 |
必選 |
預設值 |
可選值 |
備忘 |
| chdir(2.4後新增) |
no |
|
|
運行command命令前先cd到這個目錄 |
| creates |
no |
|
|
如果這個參數對應的檔案存在,就不運行command |
| decrypt |
no |
yes |
yes/no |
此選項控制使用保存庫的源檔案的自動解密 |
| free_form |
yes |
|
|
需要執行指令碼的本地檔案路徑(沒有真正的參數為free_form) |
| removes |
no |
|
|
如果這個參數對應的檔案不存在,就不運行command,與creates參數的作用相反 |
樣本
[[email protected] ~]# ansible test -m script -a "test.sh chdir=/tmp"172.20.21.120 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 172.20.21.120 closed.\r\n", "stdout": "/tmp\r\n", "stdout_lines": [ "/tmp" ]}
注意事項
- 通常來說,使用Ansible模組比推送指令碼更好
- 當指令碼執行時,ssh串連外掛程式將通過
-tt強制偽tty分配。偽ttys沒有stderr通道,所有stderr被發送到標準輸出。如果需要標準輸出和標準錯誤分離,請使用到copy模組。
raw模組簡介
raw模組主要用於執行一些低級的,髒的SSH命令,而不是通過command模組。 raw模組只適用於下列兩種情境,第一種情況是在較老的(Python 2.4和之前的版本)主機上,另一種情況是對任何沒有安裝Python的裝置(如路由器)。 在任何其他情況下,使用shell或command模組更為合適。
- 就像
script模組一樣,raw模組不需要遠程系統上的python
模組參數
| 名稱 |
必選 |
備忘 |
| executable |
no |
改變用來執行命令的shell,應該是可執行檔的絕對路徑。 |
| free_form |
yes |
需要執行的指令碼(沒有真正的參數為free_form) |
樣本
[[email protected] ~]# ansible test -m raw -a "pwd"172.20.21.120 | SUCCESS | rc=0 >>/rootShared connection to 172.20.21.120 closed.
注意事項
- 如果要安全可靠地執行命令,最好使用
shell或command模組來代替。
- 如果從playbook中使用raw,則可能需要使用
gather_facts: no禁用事實收集
expect模組簡介
expect模組用於在給的的節點上執行一個命令並響應提示。
- 它不會通過shell處理命令,因此不支援像
$HOME這樣的變數和,以及<, >, |, ;和&等都是無效的。也就是在command模組中無法使用管道符。
使用要求(在執行模組的主機上)
- python >= 2.6
- pexpect >= 3.3
模組參數
| 名稱 |
必選 |
預設值 |
備忘 |
| chdir |
no |
|
運行command命令前先cd到這個目錄 |
| command |
yes |
|
命令模組執行命令運行 |
| echo |
no |
|
是否回顯你的回應字串 |
| responses |
yes |
|
期望的字串/Regex和字串的映射來響應。 如果響應是一個列表,則連續的匹配將返回連續的響應。 列表功能是2.1中的新功能。 |
| creates |
no |
|
如果這個參數對應的檔案存在,就不運行command |
| removes |
no |
|
如果這個參數對應的檔案不存在,就不運行command,與creates參數的作用相反 |
| timeout |
no |
30 |
以秒為單位等待預期時間 |
樣本
- name: Case insensitve password string match expect: command: passwd username responses: (?i)password: "MySekretPa$$word"- name: Generic question with multiple different responses expect: command: /path/to/custom/command responses: Question: - response1 - response2 - response3
注意事項
- 如果你想通過shell運行一個命令(比如你正在使用
<,>,|等),你必須在命令中指定一個shell,比如/bin/bash -c "/path/to/something | grep else"。
- 在
responses下關鍵是一個pythonRegex匹配,不區分大小寫搜尋用首碼?i。
- 預設情況下,如果多次遇到問題,則會重複其字串響應。 如果連續問題匹配需要不同的響應,而不是字串響應,請使用字串列表作為響應。
expect模組設計用於簡單情境,對於更複雜的需求,應該考慮在shell或script模組中使用expect代碼
telnet模組簡介
expect模組用於執行一些低級的和髒telnet命令,不通過模組子系統。
- 它不會通過shell處理命令,因此不支援像
$HOME這樣的變數和,以及<, >, |, ;和&等都是無效的。也就是在command模組中無法使用管道符。
模組參數
| 名稱 |
必選 |
預設值 |
備忘 |
| command |
yes |
|
在telnet會話中執行的命令 |
| host |
no |
remote_addr |
要執行命令的主機/目標 |
| password |
yes |
|
登入密碼 |
| pause |
no |
1 |
每發出一個命令之間的暫停秒 |
| port |
no |
23 |
遠程連接埠 |
| prompts |
no |
[u‘$‘] |
發送下一個命令之前預期的提示列表 |
| timeout |
no |
30 |
遠程操作逾時時間 |
| user |
no |
remote_user |
登入使用者 |
樣本
- name: send configuration commands to IOS telnet: user: cisco password: cisco login_prompt: "Username: " prompts: - "[>|#]" command: - terminal length 0 - configure terminal - hostname ios01- name: run show commands telnet: user: cisco password: cisco login_prompt: "Username: " prompts: - "[>|#]" command: - terminal length 0 - show version
注意事項
- 如果你想通過shell運行一個命令(比如你正在使用
<,>,|等),你必須在命令中指定一個shell,比如/bin/bash -c "/path/to/something | grep else"。
- 在
responses下關鍵是一個pythonRegex匹配,不區分大小寫搜尋用首碼?i。
- 預設情況下,如果多次遇到問題,則會重複其字串響應。 如果連續問題匹配需要不同的響應,而不是字串響應,請使用字串列表作為響應。
expect模組設計用於簡單情境,對於更複雜的需求,應該考慮在shell或script模組中使用expect代碼
hoxis
連結:https://www.jianshu.com/p/8661c107448d
來源:簡書
Ansible 命令相關模組command, shell, raw, expect, script, telnet[轉]