一、模塊簡介
ios_command此模塊將任意命令發送到ios節點並返回設備讀取的結果
此模塊不支持在配置模式下使用,即只支持在用戶模式>和特權模式#下使用
官方文檔地址:https://docs.ansible.com/ansible/latest/modules/ios_command_module.html#ios-command-module
二、模塊參數
auth_pass #進入特權模式使用的密碼,如果authorize=no,則不檢索此特權密碼; #如果authorize=yes,而auth_pass不填,則使用環境變量ANSIBLE_NET_AUTH_PASS的值作為特權密碼進行驗證 authorize #no or yes,是否進去特權模式,默認使用環境變量ANSIBLE_NET_AUTHORIZE的值,默認在用戶模式下 command #【必填項】需要發送到遠端執行的命令列表,如果遇到需要回答提示,則在命令后使用參數prompt:提示關鍵字,使用參數answer:'y'回答或者answer:'\r',或者answer:'n' interval #重試之間的等待間隔時間,默認是1s wait_for #在繼續前進之前,任務會等待條件成立,如果不成立,則任務失敗。 match #與wait_for配合使用,默認是all,可選參數all或any,match=all表示必須滿足wait_for里的所有條件;match=any表示只要滿足wait_for里的任一條件即可 retries #重試次數,默認10次 provider #包含如下dict對象 auth_pass #進入特權模式的密碼,與上面auth_pass效果一樣 authorize #no or yes,默認no,與上面authorize效果一樣 host #【必填項】填寫域名或者ip地址,需要連接的設備地址,{{ansible_host}}代表遍歷/etc/ansible/hosts文件里每行中帶有ansible_host的主機 username #遠程登錄用戶名,默認使用環境變量ANSIBLE_NET_USERNAME password #遠程登錄密碼,默認使用環境變量ANSIBLE_NET_PASSWORD port #默認22端口 ssh_keyfile #ssh密鑰文件位置,默認使用環境變量ANSIBLE_NET_SSH_KEYFILE timeout #超時時間,默認10s
三、模塊使用注意事項
暫無
四、模塊使用實例
4.1 查看ios組里的主機的版本號
a.編輯/etc/ansible/hosts文件,新增如下內容
[ios] r5 ansible_host=192.168.xx.45 r6 ansible_host=192.168.xx.46 r7 ansible_host=192.168.xx.47 r8 ansible_host=192.168.xx.48
b.編寫ansible playbook劇本nano ios_example01.yaml,嚴格按照如下格式
--- - name: ios command module example hosts: ios connection: local gather_facts: no tasks: - name: check ios version ios_command: commands: show version provider: username: cisco authorize: yes auth_pass: cisco host: "{{ansible_host}}" password: cisco register: show_version_output - name: print the show version debug: msg: "{{show_version_output.stdout_lines}}"
c.如果第一次登錄設備,需要修改nano /etc/ansible/ansible.cfg文件里的host_key_checking = False,即關閉主機密鑰檢測
d.執行劇本:ansible-playbook iso_example01.yaml,如下部分截圖
4.2 清除ios組里的所有主機的ether 0/0接口信息計數,並自動根據提示,回復y執行
步驟與3.1的例子一模一樣,差別在於b點的playbook劇本內容,劇本內容如下:
--- - name: ios command module example hosts: ios connection: local gather_facts: no tasks: - name: run show version and check to see if output contains IOS ios_command: commands: - command: 'clear counters Ethernet0/0 ' prompt: 'Clear "show interface" counters on this interface \[confirm\]' answer: 'y' - command: 'clear counters Ethernet0/1' prompt: '[confirm]' answer: "\r" provider: host: "{{ansible_host}}" username: cisco password: cisco authorize: yes auth_pass: cisco register: show_version_output - name: print the show version debug: msg: "{{show_version_output.stdout_lines}}"
五、模塊返回內容字段介紹
failed_conditions #失敗的條件列表,返回列表形式 stdout #遠程執行命令的返回結果,返回列表形式 stdout_lines #多條遠程執行命令,將每個命令的返回結果列表1在一一存入列表2中