아래에서 이전 응답을 바탕으로 더 구체적으로 설명하겠습니다. 사용자가 요청한 대로, NTC Templates의 templates/ 디렉터리 내 장비별/명령어별로 분류되어 있는 정보를 어떻게 사용할 수 있는지에 초점을 맞춰 설명하겠습니다. NTC Templates는 GitHub에서 다운로드할 수 있으며, 이 디렉터리는 템플릿 파일들이 체계적으로 정리되어 있어, 특정 네트워크 장비와 명령어에 맞는 템플릿을 쉽게 찾아 사용할 수 있습니다.
1. templates/ 디렉터리 구조 이해
NTC Templates의 templates/ 디렉터리는 다음과 같이 분류되어 있습니다:
- 장비별 서브디렉터리: 각 네트워크 장비 벤더나 플랫폼에 따라 서브디렉터리가 나뉩니다. 예를 들어:
cisco_ios/: Cisco IOS 기반 장비 (e.g., Cisco Catalyst, IOSv).
cisco_nxos/: Cisco Nexus OS.
juniper_junos/: Juniper Junos.
arista_eos/: Arista EOS.
- 기타: F5, HP Comware 등 다양한 벤더.
- 명령어별 파일: 각 서브디렉터리 내에서 CLI 명령어에 해당하는 템플릿 파일이
.textfsm 확장자로 저장됩니다. 파일 이름은 보통 명령어와 관련된 키워드를 포함합니다. 예:
cisco_ios_show_ip_interface_brief.textfsm: Cisco IOS 장비의 show ip interface brief 명령어 출력을 파싱하는 템플릿.
cisco_nxos_show_vlan.textfsm: Cisco Nexus의 show vlan 명령어 출력을 파싱하는 템플릿.
이 구조는 템플릿을 쉽게 탐색하고 재사용할 수 있도록 설계되었습니다. 전체 목록은 NTC Templates GitHub에서 확인할 수 있습니다.
예시 구조 (GitHub에서 실제로 보이는 것):
templates/
├── cisco_ios/
│ ├── show_ip_interface_brief.textfsm
│ ├── show_version.textfsm
│ ├── show_interfaces.textfsm
│ └── ...
├── cisco_nxos/
│ ├── show_vlan.textfsm
│ ├── show_interface_description.textfsm
│ └── ...
├── juniper_junos/
│ ├── show_interfaces.textfsm
│ └── ...
└── ...
- 장비별 분류의 이유: 각 장비의 CLI 출력 형식이 다르기 때문에, 템플릿을 장비별로 분리하여 정확한 파싱을 보장합니다.
- 명령어별 분류의 이유: 하나의 장비에서 여러 명령어를 지원하므로, 각 명령어의 출력 형식을 별도 파일로 관리합니다.
2. templates/ 디렉터리 정보를 사용하는 방법
이 디렉터리의 템플릿 파일을 사용하려면, Python 스크립트나 Ansible 플레이북에서 템플릿 파일의 경로를 지정해야 합니다. NTC Templates 라이브러리는 이 경로를 기반으로 템플릿을 로드하고, CLI 출력을 파싱합니다.
단계 1: 템플릿 다운로드 및 준비
먼저, 로컬에 다운로드하세요:
git clone https://github.com/networktocode/ntc-templates.git
cd ntc-templates
이제 templates/ 디렉터리가 로컬에 생성됩니다. 예를 들어, templates/cisco_ios/show_ip_interface_brief.textfsm 파일이 존재합니다.
단계 2: Python에서 사용하기
Python 코드에서 parse_output 함수를 사용할 때, 템플릿 파일의 경로를 암시적으로 지정합니다. NTC Templates 라이브러리는 자동으로 templates/ 디렉터리를 검색하므로, 플랫폼과 명령어를 지정하기만 하면 됩니다.
구체적인 예제: Cisco IOS의 show ip interface brief 템플릿 사용
from ntc_templates.parse import parse_output
# CLI 출력 예시 (실제 장비에서 얻은 데이터)
raw_output = """
Interface IP-Address OK? Method Status Protocol
GigabitEthernet0/0 192.168.1.1 YES NVRAM up up
GigabitEthernet0/1 unassigned YES NVRAM administratively down down
"""
# parse_output 함수 호출
# - platform: 장비 유형 (e.g., cisco_ios), 이는 templates/cisco_ios/ 디렉터리를 가리킴
# - command: 명령어 (e.g., show_ip_interface_brief), 이는 해당 디렉터리 내 파일을 검색
result = parse_output(
platform="cisco_ios", # templates/cisco_ios/ 디렉터리를 검색
command="show_ip_interface_brief", # templates/cisco_ios/show_ip_interface_brief.textfsm 파일을 사용
data=raw_output
)
print(result)
# 출력:
# [
# {"interface": "GigabitEthernet0/0", "ip_address": "192.168.1.1", "status": "up", "protocol": "up"},
# {"interface": "GigabitEthernet0/1", "ip_address": "unassigned", "status": "administratively down", "protocol": "down"}
# ]
- 작동 원리:
platform="cisco_ios"를 지정하면, 라이브러리가 templates/cisco_ios/ 디렉터리를 탐색합니다.
command="show_ip_interface_brief"를 지정하면, 그 디렉터리 내에서 show_ip_interface_brief.textfsm 파일을 자동으로 로드합니다.
- 만약 파일이 없으면 에러가 발생하므로, GitHub에서 파일 존재를 확인하세요.
단계 3: Ansible에서 사용하기
Ansible의 parse_cli 모듈을 사용할 때, template_path 매개변수를 통해 templates/ 디렉터리의 경로를 지정합니다.
구체적인 예제: Cisco IOS의 show version 템플릿 사용
- name: Parse Cisco show version output
hosts: cisco_switches
gather_facts: no
tasks:
- name: Run show version command
ios_command: # Cisco IOS 장비를 대상
commands: show version
register: output
- name: Parse output using NTC Templates
parse_cli:
content: "{{ output.stdout[0] }}" # CLI 출력
template_path: "/path/to/ntc-templates/templates" # templates/ 디렉터리 경로, e.g., /home/user/ntc-templates/templates
platform: "cisco_ios" # templates/cisco_ios/ 디렉터리를 가리킴
command: "show_version" # templates/cisco_ios/show_version.textfsm 파일을 사용
register: parsed_data
- name: Debug the parsed result
debug:
var: parsed_data
3. 템플릿 파일의 내용과 이해
각 .textfsm 파일은 TextFSM 언어로 작성된 템플릿으로, CLI 출력의 행과 열을 정규표현식으로 매핑합니다. 예를 들어, templates/cisco_ios/show_ip_interface_brief.textfsm 파일의 일부 내용은 다음과 같습니다:
Value Interface (\S+)
Value IP-Address (\S+)
Value Status (\S+)
Value Protocol (\S+)
Start
^\s*Interface\s+IP-Address\s+OK\?\s+Method\s+Status\s+Protocol -> Record
^${Interface}\s+${IP-Address}\s+\S+\s+\S+\s+${Status}\s+${Protocol}
- 작동 방식:
Value 줄: 출력에서 추출할 필드를 정의 (e.g., Interface, IP-Address).
Start 줄: 출력의 시작 부분을 식별.
- 정규표현식: 각 행을 파싱하여 데이터 추출.
사용자가 직접 템플릿을 수정하거나 새로 작성할 수 있지만, 기본적으로는 기존 파일을 사용하세요.
4. 추가 팁과 주의사항
-
템플릿 찾기:
- GitHub에서
templates/ 디렉터리를 브라우징하거나, 로컬에서 find 명령어로 검색하세요: find templates/ -name "*show_ip_interface_brief.textfsm".
- 지원되는 장비와 명령어는 NTC Templates 문서를 확인하세요.
-
커스텀 템플릿 추가:
- 기존 구조를 따르세요. 예: 새 템플릿을
templates/custom_mydevice_show_command.textfsm에 추가한 후, Python/Ansible에서 platform="custom_mydevice"와 command="show_command"로 호출.
-
주의사항:
- 템플릿 경로가 올바른지 확인: Python/Ansible에서 경로를 잘못 지정하면 파싱 실패.
- 템플릿 업데이트: 장비 소프트웨어가 업그레이드되면, GitHub에서 최신 템플릿을 pull하세요.
- 에러 처리: Python에서
parse_output가 실패하면 None을 반환하므로, try-except 블록을 사용하세요.
이 설명으로 templates/ 디렉터리의 사용 방법이 더 명확해졌기를 바랍니다. 추가 질문이 있으시면 언제든 말씀해주세요!