Ansible Callback Plugin 소개
Ansible Plugin 중에서 Callback Plugin 에 관련한 부분만 정리합니다. Callback Plugin 은 Ansible 에서 특정 이벤트 발생 시 데이터를 로깅한다거나 Slack, Mail 등의 외부 채널로 Write 하는 등의 다양한 목적을 달성하기 위해 사용하는 모듈입니다. 참고로 이 내용은 Ansible 2.2.1.0 기준으로 작성되었습니다.
Callback Plugin이란?
소개
Ansible Callback Plugin 은 Ansible 의 각종 이벤트를 Hooking 해서 해당 시점에 원하는 로직을 수행할 수 있는 플러그인을 말합니다. 이 콜백 플러그인은 Ansible Task, Playbook 등에 대해 “실행 직전”, “실행 종료” 등의 이벤트에 대한 콜백 함수를 정의할 수 있도록 지원합니다.
활용 사례
Callback Plugin은 다양한 용도로 활용할 수 있습니다:
- 성능 모니터링: 태스크 실행 시간 측정
- 알림: Slack, 이메일, SMS 등으로 실행 결과 전송
- 로깅: 상세한 실행 로그를 파일이나 DB에 저장
- 메트릭 수집: Prometheus, Datadog 등으로 메트릭 전송
- 감사: 실행 기록 보존 및 추적
동작 방식
기본적으로 Callback Plugin 들은 callback_whitelist 라는 Ansible 환경 변수에 등록된 플러그인에 대해서만 콜백 함수가 동작하도록 되어 있습니다. 단, 콜백 모듈을 CALLBACK_NEEDS_WHITELIST = False 로 설정한 경우에는 무관합니다.
그리고, Callback Plugin 의 실행 순서는 Alphanumeric 순으로 실행됩니다. (e.g. 1.py → 2.py → a.py) 설정에 등록된 콜백 리스트 순서와는 무관합니다.
환경 설정
Callback Plugin 을 사용하기 위한 각종 Ansible 환경 설정을 정리합니다. 이 환경변수들은 ansible.cfg 파일에 정의해서 사용해도 되며, 커맨드라인을 통해 전달하는 방식도 가능합니다.
주요 설정 변수
- callback_plugins : 콜백 플러그인이 있는 디렉토리 위치를 지정합니다.
(ex) callback_plugins = ~/.ansible/plugins/callback:/usr/share/ansible/plugins/callback
- stdout_callback : stdout 에 대한 기본 콜백을 변경합니다. CALLBACK_TYPE = stdout 인 콜백 플러그인 모듈만 지정이 가능합니다.
(ex) stdout_callback = skippy
- callback_whitelist : 콜백을 동작시킬 플러그인 이름을 지정합니다. CALLBACK_NEEDS_WHITELIST = False 인 콜백 플러그인 모듈은 무관합니다.
(ex) callback_whitelist = timer,mail
ansible.cfg 예시
| |
커맨드라인 옵션
| |
이벤트 후킹 (Event Hooking)
Ansible 프로젝트의 “lib/ansible/plugins/callback/__init__.py” 의 소스에 존재하는 CallbackBase 클래스의 Public Method가 이벤트 후킹 가능한 콜백 함수를 의미합니다.
Callback Plugin 을 구현하는 경우, CallbackBase 클래스를 상속해서 사용할 이벤트를 Override 하시면 됩니다. 만약, Ansible 2.0 버전 이상에 해당하는 이벤트에만 콜백 함수가 동작하기를 원하하는 경우에는 함수에 “v2_” 접두어를 붙인 메소드를 Override 하시면 됩니다.
이벤트 종류
| |
구현 예제
아래의 Ansible Plugin 은 [jlafon/ansible-profile] 프로젝트를 차용하였다는 점을 먼저 알려드립니다.
이 플러그인에 대해서 간략하게 설명하자면, playbook의 task의 수행 시간을 메모리에 적재한 뒤에 playbook이 종료되기 전 태스크의 수행 시간을 Display 해주는 간단한 플러그인 입니다.
| |
실행 예시
| |
고급 활용 예제
Slack 알림 플러그인
| |
로그 파일 기록 플러그인
| |
메트릭 수집 플러그인
| |
표준 Callback Plugin 목록
Ansible은 다양한 표준 Callback Plugin을 제공합니다:
| Plugin | Type | Description |
|---|---|---|
| actionable | notification | Show only tasks that need action |
| aws_resource_tags | notification | Add AWS resource tags to resources |
| cgroup_memory_recap | aggregate | Profile max memory usage |
| cgroup_perf_recap | aggregate | Profile system activity |
| context_demo | stdout | Demo plugin |
| counter_enabled | stdout | Count tasks |
| debug | stdout | Debug output |
| default | stdout | Default output |
| dense | stdout | Minimal output |
| foreman | notification | Send events to Foreman |
| full_skip | stdout | Show skipped tasks |
| hipchat | notification | Send events to HipChat |
| jabber | notification | Send events to Jabber |
| json | stdout | JSON output |
| junit | aggregate | Write JUnit XML |
| log_plays | aggregate | Log playbook results |
| logdna | notification | Send events to LogDNA |
| logentries | notification | Send events to Logentries |
| logstash | aggregate | Send events to Logstash |
| notification | Send email | |
| nrdp | notification | Send events to NRDP |
| null | stdout | No output |
| oneline | stdout | One line output |
| osx_say | notification | Use macOS say command |
| profile_roles | aggregate | Profile roles |
| profile_tasks | aggregate | Profile tasks |
| say | notification | Use say command |
| selective | stdout | Selective output |
| skippy | stdout | Hide skipped tasks |
| slack | notification | Send events to Slack |
| splunk | notification | Send events to Splunk |
| stderr | stdout | Output to stderr |
| sumologic | notification | Send events to Sumologic |
| syslog_json | aggregate | JSON to syslog |
| timer | aggregate | Profile time |
| tree | stdout | Tree output |
| unixy | stdout | Unix-style output |
| yaml | stdout | YAML output |
모범 사례
1. CALLBACK_TYPE 올바르게 설정
| |
2. 환경변수 활용
| |
3. 에러 처리
| |
4. v2 API 사용
| |