需要了解有關 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才能使其運作。

相關內容