使用處理程序時 ansible 中出現語法錯誤

使用處理程序時 ansible 中出現語法錯誤

下面是我的角色任務,它複製了 nginx conf,然後僅當 nginx -t 的 shell 命令輸出包含“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

相關內容