Ansible - Handler > Ansible 자료실

본문 바로가기
사이트 내 전체검색

Ansible 자료실

운영 Ansible - Handler

페이지 정보

profile_image
작성자 AnsibleM
댓글 0건 조회 11,110회 작성일 21-03-26 14:39

본문

Handler

- 시스템에서 변경이 있을 때만 작업이 실행되는 경우 사용되며 대부분 service restart 등에 사용됨
- 예를 들어, apche의 여러 설정 파일들이 변경된 경우 handler 를 활용하여 apache 재시작 가능
- task의 맨 뒤에 notify action 설정(handler의 이름으로 호출
- 여러 번 조건을 만족해도 단 한번만 실행됨


1. 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
---
- name: Verify apache installation
  hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: Ensure apache is at the latest version
    yum:
      name: httpd
      state: latest
 
  - name: Write the apache config file
    template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf
    notify:
    - Restart apache
 
  - name: Ensure apache is running
    service:
      name: httpd
      state: started
 
  handlers:
    - name: Restart apache
      service:
        name: httpd
        state: restarted
cs
> apache 설정 파일 수정 후 handler 호출 > apache 재시작

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- name: Template configuration file
  template:
    src: template.j2
    dest: /etc/foo.conf
  notify:
    - Restart memcached
    - Restart apache
 
  handlers:
    - name: Restart memcached
      service:
        name: memcached
        state: restarted
 
    - name: Restart apache
      service:
        name: apache
        state: restarted
cs
> 하나의 task는 둘 이상의 handler 호출 가능


2. handler 실행 시기 제어

기본적으로 handler 는 모든 task 가 작업 완료된 이후에 수행되는데, handler 는 한 번만 수행되기 때문에 이러한 방식이 효율적임.
하지만 play 가 끝나기 전에 handler 를 수행해야 하는 경우 ansible 작업을 실행하는 meta 모듈을 활용하여 handler 를 실행할 수 있음.

1
2
3
4
5
6
7
- template:
    src: new.j2
    dest: /etc/config.txt
  notify: myhandler
 
- name: Force all notified handlers to run at this point, not waiting for normal sync points
  meta: flush_handlers
cs



3. handler 와 변수 사용

예를 들어, 서비스 이름이 다를 경우 각 대상 시스템에 대해 다시 시작된 서비스의 정확한 이름을 출력하고자 할 때 handler name 에 변수를 넣는 것은 에러가 발생할 수 있음.

1
2
3
handlers:
# This handler name may cause your play to fail!
- name: Restart "{{ web_service_name }}"
cs
> 해당 변수를 사용할 수 없을 경우 전체 task 수행이 실패하게 됨

1
2
3
4
5
6
7
8
9
tasks:
  - name: Set host variables based on distribution
    include_vars: "{{ ansible_facts.distribution }}.yml"
 
handlers:
  - name: Restart web service
    ansible.builtin.service:
      name: "{{ web_service_name | default('httpd') }}"
      state: restarted
cs
> handler 의 task 매개 변수에 변수 설정 가능

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
handlers:
  - name: Restart memcached
    service:
      name: memcached
      state: restarted
    listen: "restart web services"
 
  - name: Restart apache
    service:
      name: apache
      state: restarted
    listen: "restart web services"
 
tasks:
  - name: Restart everything
    command: echo "this task will restart the web services"
    notify: "restart web services"
cs
> listen 옵션 활용 > task 에서 handler trigger 가능

댓글목록

등록된 댓글이 없습니다.

회원로그인

회원가입

사이트 정보

회사명 : (주)리눅스데이타시스템 / 대표 : 정정모
서울본사 : 서울특별시 강남구 봉은사로 114길 40 홍선빌딩 2층 / tel : 02-6207-1160
대전지사 : 대전광역시 유성구 노은로174 도원프라자 5층 / tel : 042-331-1161

접속자집계

오늘
825
어제
1,504
최대
3,935
전체
793,684
Copyright © www.linuxdata.org All rights reserved.