[ Handler ]
- 핸들러는 notify에 의해 호출되며 주로 서비스 재시작, 캐시클리어 등 후속 작업을 처리합니다.
- 별로의 handler 폴더에 정의되어 실행할 수 있습니다.
- 핸들러는 플레이북 실행 종료 시점에 호출됩니다.
- 동일한 핸들러가 여러 태스크에서 호출되더라도 한번만 실행합니다.
- 실행 순서는 notify에 나열된 순서가아니라 handler에 선언된 순서로 실행됩니다.
[ Handler Example ]
tasks:
- name: Template configuration file
ansible.builtin.template:
src: template.j2
dest: /etc/foo.conf
notify:
- Restart apache
- Restart memcached
handlers:
- name: Restart memcached
ansible.builtin.service:
name: memcached
state: restarted
- name: Restart apache
ansible.builtin.service:
name: apache
state: restarted
Template configuration file 태스크가 change 상태이면, handlers 섹션에 정의된 내용이 실행됩니다.
[ Notifying and loops ]
tasks:
- name: Template services
ansible.builtin.template:
src: "{{ item }}.j2"
dest: /etc/systemd/system/{{ item }}.service
# Note: if *any* loop iteration triggers a change, *all* handlers are run
notify: Restart {{ item }}
loop:
- memcached
- apache
handlers:
- name: Restart memcached
ansible.builtin.service:
name: memcached
state: restarted
- name: Restart apache
ansible.builtin.service:
name: apache
state: restarted
- loop를 통해 {{item}}.j2 대상이 되는 서비스는 memcached.service, apache.service 파일입니다.
- 대상이 되는 템플릿 파일 중 하나라도 변경되면 memcached, apache 둘 다 재시작됩니다.
- loop에 nginx를 추가하여 쉽게 확장 가능합니다.
[ Controlling when handlers run ]
핸들러는 기본적으로 모든 플레이북의 태스크가 완료된 후에 실행하여
핸들러를 중복으로 호출해도 한번만 실행되는 효율적인 프로세스를 가지고 있습니다.
그러나 플레이북이 끝나기전에 핸들러를 실행해야 한다면 "meta"모듈을 사용하여
강제로 핸들러를 실행하도록 할 수 있습니다.
tasks:
- name: Some tasks go here
ansible.builtin.shell: ...
- name: Flush handlers
meta: flush_handlers
- name: Some other tasks
ansible.builtin.shell: ...
[ When ]
- when은 조건부 실행을 위해 사용되며, 참(True)일 때만 태스크나 핸들러를 실행합니다.
[ When Example ]
기본 구조
- name: Task description
module_name:
parameter1: value1
parameter2: value2
when: condition
복수 조건
tasks:
- name: "shut down CentOS 6 and Debian 7 systems"
command: /sbin/shutdown -t now
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "6") or
(ansible_facts['distribution'] == "Debian" and ansible_facts['distribution_major_version'] == "7")
ansible이 가지는 고유한 테스트 문구(failed, succeeded, skipped)를 이용
tasks:
- command: /bin/false
register: result
ignore_errors: True
- command: /bin/something
when: result is failed
- command: /bin/something_else
when: result is succeeded
- command: /bin/still/something_else
when: result is skipped
등록된 변수(register)와 함께 상용
- name: Check if a file exists
stat:
path: /path/to/file
register: file_status
- name: Delete the file if it exists
file:
path: /path/to/file
state: absent
when: file_status.stat.exists
with_items로 루프 내 조건만 실행
- name: Install packages
yum:
name: "{{ item }}"
state: present
with_items:
- httpd
- nginx
- mysql
when: item != "nginx"
이외에도
group_var에 정의된 변수로 조건을 만들거나,
핸들러에서도 사용할 수 있습니다.
[ 참조 ]
handler :
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_handlers.html
when :
https://docs.ansible.com/ansible/2.9/user_guide/playbooks_conditionals.html
'Infra > Ansible' 카테고리의 다른 글
Ansible - Inventory Build (0) | 2024.12.20 |
---|---|
Ansible - Playbook Roles 작성 (0) | 2023.03.27 |
Ansible - ssh-key를 통한 비밀번호 없이 ssh접속 (0) | 2023.03.26 |
Ansible - 설치 및 실행 (0) | 2023.03.26 |