Ansible의 핸들러에 대해 더 많이 알아야 합니다.

Ansible의 핸들러에 대해 더 많이 알아야 합니다.

다음은 nginx 역할에서 수행하는 작업입니다.

# 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")

처리기가 command_output 변수를 읽을지는 확실하지 않습니다.

답변1

command_output핸들러에 존재하지 않기 때문에 그렇지 않습니다 . 검사 작업의 오류 처리를 수정할 수 있으며"변경된"을 정의귀하의 조건과 일치하지만 이 경우 validate에는template작업을 수행하고 나머지는 건너뜁니다.

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

Reload nginx참고: 이 작업을 수행하려면 처리기의 이름을 로 변경해야 합니다 .

관련 정보