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注:これを機能させるには、ハンドラーの名前を変更する必要があります。

関連情報