
以下は、nginx conf をコピーし、nginx -t のシェル コマンド出力に「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