앤시블 네트워크 자동화 -5
ansible 네트워크 telnet 사용하기
오래된 네트워크 장비는 ssh를 지원하지 않을 수도 있다. 이런 경우에, telnet을 사용해야 하며, ansible.netcommon 컬렉션의 일부분인 플러그인을 사용하면 된다.
ansible-galaxy 명령으로 해당 컬렉션을 설치한다. 이미 설치되어 있으면 아래와 같은 메시지가 보인다.
$ ansible-galaxy collection install ansible.netcommon Process install dependency map Starting collection install process Skipping 'ansible.netcommon' as it is already installed
참고문서(https://docs.ansible.com/ansible/latest/collections/ansible/netcommon/telnet_module.html)의 예제를 기본으로 플레이북을 작성하였으나, 작동하지 않는다.
---
- name: Telnet test....
ansible.netcommon.telnet:
user: cisco
password: cisco
login_prompt: 'Username: '
prompts:
- '[>#]'
command:
- terminal length 0
- show version
플레이북을 실행하면 다음 에러가 발생한다.
$ ansible-playbook get_version.yml -i hosts.yml ERROR! 'ansible.netcommon.telnet' is not a valid attribute for a Play
두번째 문서(블로그)를 참고하여 플레이북을 아래와 처럼 수정했다.
$ cat get_arp.yml
---
- name: Telnet test
connection: local
gather_facts: false
hosts: all
tasks:
- name: telnet test...
ansible.netcommon.telnet:
user: admin
password: adminpasswd
login_prompt: 'Username: '
password_prompt: 'Password: '
prompts:
- '[>|#]'
command:
- terminal length 0
- show version
register: version
- name: output
debug:
msg: "{{ version }}"
인벤토리는 다음처럼 작성했다.
$ cat hosts.yml
---
switches:
hosts:
LAN1:
ansible_host: 192.168.250.1
LAN2:
ansible_host: 192.168.210.1
vars:
ansible_network_os: cisco.ios.ios
#ansible_user: admin
wifi:
hosts:
WIFI:
ansible_host: 192.168.245.25
vars:
ansible_network_os: extreme.exos.exos
이제, 플레이북을 실행하면 잘 된다.
$ ansible-playbook get_arp.yml -i hosts.yml -l LAN1
PLAY [Get ARP information from ios] ****************************************************************************
TASK [telnet test...] ******************************************************************************************
changed: [LAN1]
TASK [output] **************************************************************************************************
ok: [LAN1] => {
"msg": {
"changed": true,
"failed": false,
"output": [
"terminal length 0\r\nlib_4507#",
"show version\r\nCisco IOS Software, Catalyst 4500 L3 Switch Software (cat4500-ENTSERVICES-M), Version 12.2(54)SG, RELEASE SOFTWARE (fc3)\r\nTechnical Support: http://www.cisco.com/techsupport\r\nCopyright (c) 1986-2010 by Cisco Systems, Inc.\r\nCompiled Sun 27-Jun-10 04:53 by prod_rel_team\r\nImage text-base: 0x10000000, data-base: 0x121F5B44\r\n\r\nROM: 12.2(20r)EW1\r\nDagobah Revision 226, Swamp Revision 31\r\n\r\nlib_4507 uptime is 6 weeks, 20 hours, 45 minutes\r\nUptime for this control processor is 6 weeks, 20 hours, 44 minutes\r\nSystem returned to ROM by power-on\r\nSystem restarted at 17:16:50 KST Sun Feb 21 2021\r\nSystem image file is \"bootflash:cat4500-entservices-mz.122-54.SG.bin\"\r\n\r\ncisco WS-C4507R (MPC8245) processor (revision 14) with 524288K bytes of memory.\r\nProcessor board ID FOX101502JB\r\nMPC8245 CPU at 333Mhz, Supervisor IV\r\nLast reset from PowerUp\r\n3 Virtual Ethernet interfaces\r\n62 Gigabit Ethernet interfaces\r\n403K bytes of non-volatile configuration memory.\r\n\r\nConfiguration register is 0x2102\r\n\r\nlib_4507#"
]
}
}
PLAY RECAP *****************************************************************************************************
LAN1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
이제, telnet만 지원되는 구형장비에도 앤시블 사용이 가능하게 되었다.
참고문서:
https://docs.ansible.com/ansible/latest/collections/ansible/netcommon/telnet_module.html
https://blog.gainskills.top/2018/09/04/ansible-access-network-via-telnet/