[Ansible] modify banner phrase and Change delimiting character '@'
Ansible을 활용하여 Cisco IOS 장비의 배너 문구를 구성할 수 있다.
구성방법은 원격 파일을 사용하거나 직접 입력하는 방법이 있다.
1. file을 통해 banner 업데이트
---
- hosts: Switch
gather_facts: no
tasks:
- name: configure cisco SW banner
ios_banner:
banner: motd
text: "{{ lookup('file', '/etc/ansible/playbooks/raw_banner.cfg')}}"
state: present
- name: save running to startup when modified
ios_config:
save_when: modified
2. text line 직접 입력
---
- hosts: Switch
gather_facts: no
tasks:
- name: configure cisco SW banner
ios_banner:
banner: motd
text: |
This is my login banner
email : admin@mydomain.com
Unauthorized access to this device
is prohibited!
state: present
- name: save running to startup when modified
ios_config:
save_when: modified
3. 배너 삭제
- name: remove the motd banner
ios_banner:
banner: motd
state: absent
※ Ansible 배너 구분 문자 수정 ( Change delimiting character '@' )
배너 입력시 배너의 시작과 끝을 구분하는 구분자가 있으며 구분자를 통해서 어디까지가 입력할 문구인지를 정하게 된다.
스위치 장비에서 배너 뒤 ?를 치면 'c'를 입력하라고 안내를 한다. 왜 'c'를 안내하는지 이유는 정확하지 않지만 반드시 c를 사용할 필요는 없다. 내가 원하는 구분문자를 사용하면 되며, 배너 문구와 구분문자가 중복되지 않도록만 유의 하면 된다.
switch(config)#banner motd ?
LINE c banner-text c, where 'c' is a delimiting character
switch(config)#banner motd C
Enter TEXT message. End with the character 'C'.
This is my login banner
email : admin@mydomain.com
Unauthorized access to this device
is prohibited!
C
switch(config)#
그런데 Ansible에서 배너 문구에 이메일 주소를 입력할 경우 에러 메시지가 출력 되고 @이하부터는 입력이 되지 않는것을 알게 되었다. 바로 Ansible Cisco ios banner 모듈에서 사용하는 구분문자 기본값이 '@'로 되어 있어서 @를 입력하면 배너가 종료되는 것이었다.
이건 ansible 모듈 파일에서 @ 대신 다른 문구를 입력하는 것으로 쉽게 해결 할 수 있다.
모듈 파일은 /usr/lib/python2.7/site-packages/ansible/modules/network/ios/ios_banner.py 에 있으며, 109, 111 라인에서 구분문자가 '@'로 되어 있는 것을 알 수 있다.
98 def map_obj_to_commands(updates, module):
99 commands = list()
100 want, have = updates
101 state = module.params['state']
102
103 if state == 'absent' and 'text' in have.keys() and have['text']:
104 commands.append('no banner %s' % module.params['banner'])
105
106 elif state == 'present':
107 if want['text'] and (want['text'] != have.get('text')):
108 banner_cmd = 'banner %s' % module.params['banner']
109 banner_cmd += ' @\n'
110 banner_cmd += want['text'].strip()
111 banner_cmd += '\n@'
112 commands.append(banner_cmd)
113
114 return commands
해당 파일의 @를 배너에 사용하지 않는 다른 문자로 변경해 주면 쉽게 해결 된다.