1. inventory和密码加密参考

Ansible Playbook备份Juniper及Cisco设备

2. 创建playbook

- name: get cisco switches version
  hosts: cisco_switches
  gather_facts: false
  vars_files:
    - pass.yml
  vars:
    ansible_password: '{{cisco_pass}}'
    output_path: "./"
    filename: "cisco_version.csv"
  tasks:
    - name: CSV - Create file and set header
      ansible.builtin.lineinfile:
        path: "{{output_path}}/{{filename}}"
        line:
          host,version,serial
        create: true
        state: present
      run_once: true
 
    - name: Gather all legacy facts
      cisco.ios.ios_facts:
        gather_subset: all

    - name: CSV - Get Cisco facts
      set_fact:
        csv_tmp: >
          {{ inventory_hostname }}, {{ansible_net_version}}, {{ansible_net_serialnum}}

    - name: CSV - Write information into csv file
      ansible.builtin.lineinfile:
        path: "{{output_path}}/{{filename}}"
        line: "{{csv_tmp}}"

    - name: CSV - Blank lines removal
      ansible.builtin.lineinfile:
        path: "{{output_path}}/{{filename}}"
        state: absent
        regex: '^\s*$'