🔧 Ansible 실무 활용 가이드
서버 + 네트워크 자동화 | 실제 플레이북 기반 사례
📌 개요
이 글은 Ansible을 실무에서 어떻게 사용하는지 구체적인 플레이북과 함께 설명합니다.
단순 기능 나열이 아닌, 서버 보안점검 · OS 정보수집 · 네트워크 장비 백업 · Hostname 변경 등 엔지니어가 바로 적용할 수 있는 실무 패턴을 다룹니다.
💡 핵심 키워드 : Agentless(에이전트 불필요) · SSH/Network_CLI 기반 · 멀티벤더 · Infrastructure as Code(IaC)
🏗️ Ansible 실행 구조도
Control Node에서 Playbook을 실행하면, SSH/Network_CLI로 대상 장비에 명령이 전달되고, 결과는 다시 수집됩니다.
🖥️ Control Node
Ansible + Playbook + Inventory
⬇
Ansible + Playbook + Inventory
SSH / network_cli 접속
🖥️ 서버
Linux / Windows
Linux / Windows
🌐 네트워크 장비
Cisco · Juniper
Cisco · Juniper
결과 수집 (fetch / register)
📂 결과 파일 수집
⬇
후속 처리
🤖 AI 분석
보안점검 결과
보안점검 결과
📊 CSV / 리포트
OS · 펌웨어 · 백업
OS · 펌웨어 · 백업
📁 프로젝트 디렉토리 구조
실무에서 권장하는 Ansible 프로젝트 구조입니다.
ansible-project/
├── inventory/
│ ├── hosts.ini # 대상 장비 목록
│ ├── group_vars/
│ │ ├── servers.yml # 서버 그룹 변수
│ │ └── network.yml # 네트워크 그룹 변수
│ └── host_vars/
│ └── router1.yml # 개별 장비 변수
├── playbooks/
│ ├── security_audit.yml # 보안점검
│ ├── gather_info.yml # OS/펌웨어 수집
│ ├── network_backup.yml # 네트워크 백업
│ └── change_hostname.yml # Hostname 변경
├── scripts/
│ └── audit.sh # 보안점검 스크립트
├── results/ # 자동 수집 결과
├── backup/ # 네트워크 백업 파일
└── ansible.cfg # Ansible 설정
🛡️ 사례 1 : 서버 보안점검 자동화
💡 자동화 흐름
- 점검 스크립트 업로드 (copy 모듈)
- 원격 실행 (shell 모듈)
- 결과 파일 생성 확인 (stat 모듈)
- 결과 파일 수집 (fetch 모듈)
- AI 분석 (후속 스크립트 호출)
📝 플레이북 : security_audit.yml
---
- name: 서버 보안점검 자동화
hosts: servers
become: yes
tasks:
# 1단계: 점검 스크립트 업로드
- name: 보안점검 스크립트 업로드
ansible.builtin.copy:
src: ../scripts/audit.sh
dest: /tmp/audit.sh
mode: '0755'
# 2단계: 점검 스크립트 실행
- name: 보안점검 실행
ansible.builtin.shell: /tmp/audit.sh
register: audit_result
# 3단계: 결과 파일 존재 확인
- name: 결과 파일 존재 여부 확인
ansible.builtin.stat:
path: /tmp/audit_result.txt
register: result_file
# 4단계: 결과 파일 수집 (서버 → Control Node)
- name: 결과 파일 수집
ansible.builtin.fetch:
src: /tmp/audit_result.txt
dest: ../results/{{ inventory_hostname }}/
flat: no
when: result_file.stat.exists
# 5단계: AI 분석 스크립트 호출
- name: AI 분석 실행 (Control Node)
ansible.builtin.shell: >
python3 ../scripts/ai_analyze.py
../results/{{ inventory_hostname }}/audit_result.txt
delegate_to: localhost
when: result_file.stat.exists
💡 실무 팁 : fetch 모듈은 서버 → Control Node 방향으로 파일을 수집합니다. delegate_to: localhost를 활용하면 Control Node에서 후속 처리(AI 분석 등)를 수행할 수 있습니다.
📋 사례 2 : 서버 OS / 펌웨어 조사 → CSV 저장
💡 자동화 흐름
- Ansible facts 자동 수집 (OS 배포판, 버전, 커널, 아키텍처 등)
- 필요 시 추가 명령 실행 (BIOS/펌웨어 버전)
- 결과를 CSV 형식으로 Control Node에 저장
📝 플레이북 : gather_info.yml
---
- name: 서버 OS / 펌웨어 정보 수집
hosts: servers
gather_facts: yes
tasks:
# BIOS/펌웨어 버전 수집
- name: BIOS 버전 확인
ansible.builtin.shell: dmidecode -s bios-version
register: bios_ver
become: yes
ignore_errors: yes
# CSV 헤더 생성 (최초 1회)
- name: CSV 헤더 작성
ansible.builtin.lineinfile:
path: ../results/server_info.csv
line: "호스트명,OS,OS버전,커널,아키텍처,BIOS버전"
create: yes
delegate_to: localhost
run_once: true
# 각 서버 정보를 CSV에 추가
- name: 서버 정보 CSV 기록
ansible.builtin.lineinfile:
path: ../results/server_info.csv
line: >-
{{ inventory_hostname }},
{{ ansible_distribution }},
{{ ansible_distribution_version }},
{{ ansible_kernel }},
{{ ansible_architecture }},
{{ bios_ver.stdout | default('N/A') }}
delegate_to: localhost
💡 실무 팁 : gather_facts: yes로 설정하면 Ansible이 자동으로 OS, CPU, 메모리, 디스크 등 수백 가지 정보를 수집합니다. ansible_distribution, ansible_kernel 등 변수로 즉시 활용 가능합니다.
💾 사례 3 : 네트워크 장비 주기적 자동 백업
💡 자동화 흐름
- Cisco IOS/IOS-XE : ios_command 모듈
- Juniper Junos : junos_command 모듈
- 타임스탬프 파일명으로 저장
- cron 또는 AWX/Tower로 주기 실행
📝 인벤토리 : hosts.ini
[cisco_devices]
switch01 ansible_host=10.10.1.1
router01 ansible_host=10.10.1.2
[cisco_devices:vars]
ansible_network_os=cisco.ios.ios
ansible_connection=network_cli
ansible_user=admin
ansible_password=!vault_encrypted
[juniper_devices]
junos_sw01 ansible_host=10.10.2.1
junos_rt01 ansible_host=10.10.2.2
[juniper_devices:vars]
ansible_network_os=junipernetworks.junos.junos
ansible_connection=netconf
ansible_user=admin
ansible_password=!vault_encrypted
📝 플레이북 : Cisco 장비 백업
---
- name: Cisco 장비 설정 백업
hosts: cisco_devices
connection: network_cli
gather_facts: no
tasks:
- name: 현재 날짜/시간 저장
ansible.builtin.set_fact:
backup_time: "{{ lookup('pipe', 'date +%Y%m%d_%H%M%S') }}"
- name: running-config 수집
cisco.ios.ios_command:
commands:
- show running-config
register: config_output
- name: 백업 파일 저장
ansible.builtin.copy:
content: "{{ config_output.stdout[0] }}"
dest: "../backup/{{ inventory_hostname }}_{{ backup_time }}.cfg"
delegate_to: localhost
📝 플레이북 : Juniper 장비 백업
---
- name: Juniper 장비 설정 백업
hosts: juniper_devices
connection: netconf
gather_facts: no
tasks:
- name: 현재 날짜/시간 저장
ansible.builtin.set_fact:
backup_time: "{{ lookup('pipe', 'date +%Y%m%d_%H%M%S') }}"
- name: configuration 수집
junipernetworks.junos.junos_command:
commands:
- show configuration
register: config_output
- name: 백업 파일 저장
ansible.builtin.copy:
content: "{{ config_output.stdout[0] }}"
dest: "../backup/{{ inventory_hostname }}_{{ backup_time }}.cfg"
delegate_to: localhost
💡 실무 팁 : 패스워드는 반드시 ansible-vault로 암호화하세요. cron에 등록하면 매일 자동 백업이 가능합니다 :
0 2 * * * ansible-playbook -i inventory/hosts.ini playbooks/network_backup.yml
0 2 * * * ansible-playbook -i inventory/hosts.ini playbooks/network_backup.yml
🏷️ 사례 4 : 네트워크 장비 Hostname 자동 변경
💡 활용 상황
- 장비 명명 규칙 변경 시 일괄 적용
- 신규 장비 초기 설정 자동화
- Cisco / Juniper 멀티벤더 동시 지원
📝 변수 파일 : host_vars/switch01.yml
---
new_hostname: "DC1-ACC-SW01"
📝 플레이북 : Cisco Hostname 변경
---
- name: Cisco 장비 Hostname 변경
hosts: cisco_devices
connection: network_cli
gather_facts: no
tasks:
- name: Hostname 변경
cisco.ios.ios_config:
lines:
- "hostname {{ new_hostname }}"
- name: 변경 확인
cisco.ios.ios_command:
commands:
- show running-config | include hostname
register: hostname_check
- name: 결과 출력
ansible.builtin.debug:
msg: "변경 결과 → {{ hostname_check.stdout_lines }}"
📝 플레이북 : Juniper Hostname 변경
---
- name: Juniper 장비 Hostname 변경
hosts: juniper_devices
connection: netconf
gather_facts: no
tasks:
- name: Hostname 변경
junipernetworks.junos.junos_config:
lines:
- "set system host-name {{ new_hostname }}"
comment: "Ansible에 의한 Hostname 변경"
- name: 변경 확인
junipernetworks.junos.junos_command:
commands:
- show configuration system host-name
register: hostname_check
- name: 결과 출력
ansible.builtin.debug:
msg: "변경 결과 → {{ hostname_check.stdout_lines }}"
💡 실무 팁 : host_vars를 활용하면 장비별로 다른 Hostname을 지정할 수 있습니다. 변경 전 반드시 --check (Dry-Run) 모드로 사전 검증하세요 :
ansible-playbook playbooks/change_hostname.yml --check
ansible-playbook playbooks/change_hostname.yml --check
🎯 엔지니어 관점 핵심 포인트
✅
워크플로 자동화 엔진 — 스크립트 업로드 → 실행 → 결과 수집 → 분석까지 이어지는 전체 흐름을 자동화합니다.
✅
서버 + 네트워크 통합 운영 — SSH(서버)와 Network_CLI/NETCONF(네트워크) 접속을 하나의 도구로 관리합니다.
✅
멀티벤더 지원 — Cisco, Juniper, Arista 등 다양한 벤더 장비를 동일한 Playbook 구조로 자동화합니다.
✅
결과 수집 → 분석 → 개선 루프 — fetch로 수집한 데이터를 AI 또는 CSV로 가공하여 자동화 + 분석 구조를 만들 수 있습니다.
✅
Infrastructure as Code — 모든 설정이 YAML 파일로 관리되므로 Git 버전관리, 복구, 재현이 가능합니다.
✅
보안 강화 — ansible-vault로 비밀번호·인증 정보를 암호화하여 안전하게 관리합니다.
'자동화' 카테고리의 다른 글
| 네트워크 업무자동화 어떻게 할까? (1) | 2026.05.16 |
|---|