Ansible 학습 03 - handlers

이번 에는 handlers 의 용법 을 소개 하 겠 습 니 다.
일부 프로필 을 수정 하려 면 다시 불 러 오 거나 서 비 스 를 다시 시작 해 야 합 니 다. 예 를 들 어 nginx 의 listen 80 을 수정 해 야 합 니 다.listen 8080 으로 변경 하기;nginx 를 다시 불 러 와 야 합 니 다.
  • hosts: dockerremote_user: rootvars:epic: truetasks:
  • 이름: nginx. conf 지정 필드 lineinfile: dest = / data / sh / nginx. confregexp = "listen 80" line = "listen 8081;" backrefs = yesbackup = yestags: hanye3notify: restart nginx handlers:
  • name: restart nginx service: name = nginx state = reloaded enabled = yes 위 는 listen 80 을 listen 8081 로 수정 한 다음 nginx 다 중 작업 호출 이 통 하지 않 는 handlers 를 다시 불 러 옵 니 다.


  • meta 작업 의 매개 변수 값 은 flushhandlers, "meta: flush handlers" 는 이전의 task 에 대응 하 는 handler 를 즉시 실행 하 는 것 을 의미 합 니 다. 무슨 뜻 입 니까?즉, 현재 meta 작업 전에 모두 두 개의 작업 이 있 습 니 다. task 1 과 task 2 는 모두 대응 하 는 handler 가 있 습 니 다. task 1 과 task 2 를 실행 한 후에 대응 하 는 handler 를 즉시 실행 합 니 다. 기본 적 인 상황 처럼 모든 작업 을 수행 한 후에 야 각 handler 를 실행 할 수 있 습 니 다.
         - hosts: docker
          remote_user: root
          vars:
            epic: true
          tasks:
            - name: gouuradd erp
              group: name=erp gid=1008
              notify: 
              useradd erp
        - meta: flush_handlers
        - name:   nginx.conf    
          lineinfile:
        dest=/data/sh/nginx.conf
          regexp="listen 9080"
          line="listen 8081\;"
          backrefs=yes
        backup=yes
         tags: hanye3
        notify:
            restart nginx
    handlers:
      - name: restart nginx
        service: name=nginx state=restarted
      - name: useradd erp
        user: name=erp uid=1008 group=erp  password="$6$O9KDopiuPgvkrb4z$ee7/lfljSe79zQLIvDROsFM23hAJRRvVc.JoBWPGW/erp shell=/bin/bash"

    여러 handlers 호출
    하나의 task 가 여러 개의 handler 를 호출 하려 고 할 때 가장 먼저 생각 하 는 것 은 하나의 handler 가 여러 개의 name 을 써 서 이 루어 질 수 있다 는 것 입 니 다. 그러나 이것 은 통 하지 않 습 니 다. 하나의 handler 는 하나의 handler name 만 정의 할 수 있 습 니 다. 여러 개의 handler 를 호출 하려 면 여러 개 를 한 그룹 (listen) 으로 나 누 어야 합 니 다.
  • name: groupadd erp2group: name=erp2 gid=3008notify:data dir
  • name: copy nginx.confcopy: src=/usr/local/nginx/conf/nginx.conf dest=/data/sh/nginx.conf
  • 이름: nginx. conf 지정 필드 lineinfile: dest = / data / sh / nginx. confregexp = "listen 9080" line = "listen 8081 \ \;" backrefs = yesbackup = yestags: hanye3notify: restart nginx handlers:
  • name: restart nginxservice: name=nginx state=restarted
  • name: useradd erpuser: name=erp uid=1008 group=erp password="$6$O9KDopiuPgvkrb4z$ee7/lfljSe79zQLIvDROsFM23hAJRRvVc.JoBWPGW3WbeHBQVG.IFcg1r8RppAIZ8naxckFGvOX2CIiwY6hEw0" home=/home/erp shell=/bin/bash""
  • name: soft dirlisten: data dirfile: path=/data/backup state=directory mode=644
  • name: home dirlisten: data dirfile: path=/data/homeerp state=directory mode=644

  • 좋은 웹페이지 즐겨찾기