hosts文件放的是主機資產信息;ios
secrets.yml是放置用戶名和密碼的;dom
success_backup.yml 是要執行的yaml文件;ssh
192.168.60.120.txt 是執行後生產的文件。ide
#hostsspa
[ios_device]debug
192.168.60.120blog
#secrets.ymlip
---ci
creds:input
username: cisco
password: cisco
auth_pass: cisco
#success_backup.yml
---
- hosts: ios_device
gather_facts: no
connection: local
tasks:
- name: OBTAIN LOGIN CREDENTIALS #獲取登陸憑證(用戶名和密碼)
include_vars: secrets.yml
- name: DEFINE PROVIDER #定義一個provider
set_fact:
provider:
host: "{{ inventory_hostname }}"
username: "{{ creds['username'] }}"
password: "{{ creds['password'] }}"
auth_pass: "{{ creds['auth_pass'] }}"
- name: show run #查看配置文件
ios_command:
provider: "{{ provider }}"
authorize: yes
commands:
- show run
register: output
- debug: var=output.stdout_lines #將output變量輸出
- name: save the output content to a file #將內容放到control機本地
local_action: copy content={{ output.stdout_lines }} dest="./{{ inventory_hostname }}.txt"
————————————————————————————————————————
注意:
1.Ansible所在的主機要可以跟cisco ios設備進行ssh通訊
2.cisco ios 配置 ssh過程:
ip domain-name [cisco.com] //cisco.com這個按我的實際狀況填寫
cryto key generate
line vty 0 4
login local
transparent input ssh
ip ssh time-out 120 //修改超時時間
ip ssh authentication-retries 1 //修改重認證次數。
ip ssh version 2 //修改版本