Ansible – Backup Cisco

$ vi ansible.cfg
[defaults]
inventory = /home/backup/conf/cisco/ansible/hosts
remote_tmp = /home/backup/conf/cisco/ansible/ansible-${USER}/tmp
local_tmp = /home/backup/conf/cisco/ansible/tmp/ansible-${USER}/tmp
host_key_checking = False
timeout = 15
log_path = /home/backup/conf/cisco/ansible/ansible.log
$ vi hosts
[cisco]
# -- cyber
CYBER_ASA_1 ansible_host=10.50.0.2
CYBER_SW_1 ansible_host=172.31.0.253
CYBER_SW_2 ansible_host=172.31.0.249

# -- jakarta
JKT_SW_R.SERVER_1 ansible_host=192.168.90.1

# -- bandung
BDG_SW_R.SERVER_LT2-48     ansible_host=192.168.2.2
#BDG_SW_R.SERVER_LT4-24     ansible_host=192.168.2.7
BDG_SW_R.SERVER_LT4-48     ansible_host=192.168.2.1
BDG_SW_R.SERVER_LAMA-48     ansible_host=192.168.2.3
BDG_SW_R.SERVER_LAMA-24     ansible_host=192.168.2.4

# -- bali

[cisco:vars]
ansible_user = agink
ansible_password = ...[sensor]...
ansible_port = 22
ansible_connection = network_cli
ansible_network_os = ios
gather_facts = false
$ vi cisco_backup.yml
---
- name: AGINK - Cisco Backup Automation
  hosts: cisco
  connection: local
  gather_facts: no
  ignore_errors: yes

  vars:
    date: "{{ lookup('pipe', 'date +%Y%m%d_%H%M') }}"
    backup_path: "/home/backup/conf/cisco"

  tasks:
    - name: show running tech
      block:
      - name: get show tech
        ios_command: 
            commands: "show tech-support" 
        register: config

      - name: create directory
        file:
          path: "{{  backup_path }}/{{ inventory_hostname }}"
          state: directory

      - name: copy config to backup folder
        copy: 
          content: "{{ config.stdout[0] }}"
          dest: "{{ backup_path }}/{{ inventory_hostname }}/running-config_{{ date }}.txt"
run
$ ansible-playbook cisco_backup.yml