hosts文件放的是主机资产信息;

secrets.yml是放置用户名和密码的;

success_backup.yml 是要执行的yaml文件;

192.168.60.120.txt 是执行后生产的文件。

 

 

#hosts

[ios_device]

192.168.60.120

 

 

#secrets.yml

---

creds:

  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          //修改版本