在 ansible 中使用角色时处理程序不起作用

在 ansible 中使用角色时处理程序不起作用

这是我的 lamp/tasks/main.yaml 文件(lamp 是角色名称)

- import_task: httpd.yaml
- import_task: php.yaml
- notify: restart httpd

这是我的 lamp/handlers/main.yaml 文件

 - name: restart httpd
   service:
    name: httpd
    state: restarted
    enabled: yes

我的主要剧本文件是:

  - hosts: server1
    remote_user: root
    roles:
       - lamp

执行剧本时出现以下错误:

ERROR! no action detected in task. This often indicates a misspelled module 
name, or incorrect module path.

The error appears to have been in '/playbooks/lamp/tasks/main.yaml': line 1, 
column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- import_task: httpd.yaml
  ^ here


The error appears to have been in '/playbooks/lamp/tasks/main.yaml': line 1, 
column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- import_task: httpd.yaml
  ^ here

exception type: <class 'ansible.errors.AnsibleParserError'>
exception: no action detected in task. This often indicates a misspelled 
module name, or incorrect module path.

The error appears to have been in '/playbooks/lamp/tasks/main.yaml': line 1, 
column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- import_task: httpd.yaml
  ^ here

我在 centos7 上使用 ansible 7.4。没有处理程序它是工作文件。但是使用处理程序时它不工作。我最近开始学习 ansible。请帮我找到解决方案。我不明白为什么处理程序不支持..?

答案1

所讨论的错误是因为import_task语法不正确,请改用import_tasks(复数)。

但是处理程序可以附加到任务或从任务中收到通知,并且它import_tasks本身不是任务,因此它不会执行您期望的操作。您应该从应触发服务重启(如配置更改)的单个任务中通知处理程序。

相关内容