티스토리 뷰

ansible

cisco switch config backup

fendys 2022. 2. 28. 20:38
반응형

test model : cisco 2960 L series

hosts - inventory

## Host 그룹 및 host 지정
## []에 그룹명을 지정해 주고 하위에 hostname 및 host ip 정보등을 입력해 주면 그룹에 host가 등록이 된다.
[cisco]
BIDC-cisco-2960-L-01 ansible_host=192.168.0.46

## 그룹에 대한 환경 변수 등록.
## [groupname:vars]형식으로 작성
[cisco:vars]
ansible_network_os=ios ###-> network OS type 정의
ansible_connection=network_cli   ###-> network OS cli 불러오기
ansible_become=yes  ###-> enable 활성화 시 필요
ansible_become_method=enable ###-> enable 활성화 시 필요


##아래 유저 확인 중 - 되면 추후 볼트로 암호화 예정
#ansible_user=admin             ## User name 입력.
#ansible_password=passwd1!      ## User password 입력.
#ansible_authorize=yes
#ansible_become_pass=password1!

 

play-book - 모듈 사용에서 필요한 커맨드 추가 방안으로 변경  

---
- hosts: cisco
  gather_facts: no

  tasks:

    - name: Create backup dir    ## 백업 Directory 경로 설정(없을 경우 자동 생성)
      file:
        path: "/data/network/config-backups/{{ lookup('pipe', 'date +%Y-%m') }}"
        state: directory
        recurse: yes

    - name: run show run
      ios_command:
        commands: show run
      register: show_run

    - name: Write
      shell: "echo '{{ inventory_hostname }} {{ show_run.stdout[0] }}' > /data/network/config-backups/{{ lookup('pipe', 'date +%Y-%m') }}/{{ inventory_hostname }}-config-{{ lookup('pipe', 'date +%Y-%m-%d') }}.txt"
      delegate_to: 127.0.0.1

    - name: run show log
      ios_command:
        commands: show log
      register: show_log

    - name: Write
      shell: "echo '{{ inventory_hostname }} {{ show_log.stdout[0] }}' > /data/network/config-backups/{{ lookup('pipe', 'date +%Y-%m') }}/{{ inventory_hostname }}-log-{{ lookup('pipe', 'date +%Y-%m-%d') }}.txt"
      delegate_to: 127.0.0.1




##other config
##### step 1
#    - name: Backup switch (ios)
#      ios_config:
#        backup: yes
#      register: backup_ios_location
#      when: ansible_network_os == 'ios'
####step 2
#    - name: Create backup dir    ## 백업 Directory 경로 설정(없을 경우 자동 생성)
#      file:
#        path: "/data/network/config-backups/{{ lookup('pipe', 'date +%Y-%m') }}"
#        state: directory
#        recurse: yes
####step 3
#    - name : copy switch config backup to backup directory
#      copy:
#       src: "{{ backup_ios_location.backup_path }}"
#       dest: "/data/network/config-backups/{{ lookup('pipe', 'date +%Y-%m') }}/{{ inventory_hostname }}-config-{{ lookup('pipe', 'date +%Y-%m-%d') }}.txt"

 

실행 스크린샷 read 전용 유저 admin으로 생성 (RSA키로 패스워드 없이도 가능 함) 

enable 활성화 시 ***** -u admin -k -K **** 로 패스워드 한번 더 입력 (위의 호스트 인벤토리에서 설정 확인 가능)

 

생성된 파일 리스트

 

컨피그 파일 확인

 

로그 파일 확인

 

 

반응형

'ansible' 카테고리의 다른 글

cisco ansible config  (0) 2024.04.05
ansible remote shell  (0) 2023.08.31
install ansible on centos 7  (0) 2021.10.26
ansible 간단 명령어  (0) 2021.08.18
ansible key copy  (0) 2021.08.05