
以下是我在 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
才能使其運作。