핸들러를 사용하는 동안 ansible의 구문 오류가 발생했습니다.

핸들러를 사용하는 동안 ansible의 구문 오류가 발생했습니다.

다음은 nginx conf를 복사한 다음 nginx -t의 쉘 명령 출력에 "syntax is ok"가 포함된 경우에만 서비스를 다시 로드하는 내 역할 작업입니다.

---
# tasks file for nginx
- name: Backup and update nginx configuration file
  template:
     src: templates/proxy.conf.j2
     dest: "{{ nginx_conf }}"
     backup: true
     owner: root
     group: root
     mode: 0644

  

- name: Running nginx -t to validate the config
  shell: 'nginx -t'
  register: command_output
  become: true
  notify: Reload nginx

- debug:
   var: command_output.stderr_lines

  handlers:
    - name: Reloading nginx service only if the syntax is ok
      systemd:
        name: nginx.service
        state: reloaded'
        when: command_output.stderr | regex_search("syntax is ok")

아래는 구문 검사 중 발생한 오류 메시지입니다. 핸들러 섹션을 주석 처리하면 제대로 작동합니다.

ERROR! conflicting action statements: debug, handlers

The error appears to be in '/home/ansible/mint-ansible/nginx/tasks/main.yaml': line 20, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- debug:
  ^ here

답변1

핸들러는 역할의 자체 하위 디렉터리에 속합니다.

- rolename
  |- tasks
  |  `- main.yml
  `- handlers
     `- main.yml

관련 정보