【问题标题】:Handlers not getting invoked after a task处理程序未在任务后被调用
【发布时间】:2018-01-22 14:10:27
【问题描述】:

我正在尝试使用 Ansible 自动执行一些任务。在我的剧本中,我有一个复制任务,然后我更改了文件的权限。完成此任务后,我需要重新启动服务。我包括 notify 并且还声明了我的处理程序,但奇怪的是这个处理程序从未被调用。

摘自我的剧本

- name: Configure Audit Log Purge Scheduler
  copy:
    src: "Scheduler-Log-Purge.config"
    dest: "{{ crx_dir }}install/com.adobe.cq.audit.purge.Scheduler-LogPurge.config"
  become: true
  tags: aem

- name: Change Permissions of the Log Purge Scheduler config File
  file:
    path: "{{ crx_dir }}install/com.adobe.cq.audit.purge.Scheduler-LogPurge.config"
    owner: crx
    group: crx
  become: true
  notify: restart aem
  tags: aem

- name: Pause the execution for cq5 to come up
  pause:
    minutes: 5
  tags: aem

这是我的处理程序文件内容。

---

- name: restart aem
  service: name=cq5 state=restarted
  become: yes

playbook运行后的o/p

gparasha-macOS:TLTD gparasha$ ansible-playbook -i hosts tltd.yml --tags aem -v
No config file found; using defaults

PLAY [Run tasks on Author] **************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************
ok: [35.169.196.183]

PLAY [Run AEM Specific Steps on Author] *************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************
ok: [35.169.196.183]

TASK [publish : Configure Audit Log Purge Scheduler] ************************************************************************************************************************************
ok: [35.169.196.183] => {"changed": false, "checksum": "3a9d00ea8357fd217a9442b1c408065abf077dfc", "failed": false, "gid": 1005, "group": "crx", "mode": "0644", "owner": "crx", "path": "/mnt/crx/author/crx-quickstart/install/com.adobe.cq.audit.purge.Scheduler-LogPurge.config", "secontext": "user_u:object_r:usr_t:s0", "size": 277, "state": "file", "uid": 1005}

TASK [publish : Change Permissions of the Log Purge Scheduler config File] **************************************************************************************************************
ok: [35.169.196.183] => {"changed": false, "failed": false, "gid": 1005, "group": "crx", "mode": "0644", "owner": "crx", "path": "/mnt/crx/author/crx-quickstart/install/com.adobe.cq.audit.purge.Scheduler-LogPurge.config", "secontext": "user_u:object_r:usr_t:s0", "size": 277, "state": "file", "uid": 1005}

TASK [publish : Pause the execution for cq5 to come up] *********************************************************************************************************************************
Pausing for 300 seconds
(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)
Press 'C' to continue the play or 'A' to abort 
fatal: [35.169.196.183]: FAILED! => {"failed": true, "msg": "user requested abort!"}

但是当我运行这个剧本时,这个服务的重启没有被调用。 为什么会这样?

我们不能在文件模块中使用通知吗? 任何帮助将不胜感激。

【问题讨论】:

    标签: ansible ansible-2.x ansible-handlers


    【解决方案1】:

    您可以将notify 附加到任何模块。

    但 Ansible 只会在任务处于更改状态时通知处理程序——这是为了防止在后续 playbook 运行时执行不必要的处理程序(例如服务重启)。

    您的日志摘录显示相关任务的"changed": false,因此不会触发处理程序执行。

    另外请记住,除非用meta 明确刷新处理程序,否则处理程序会在角色/剧本的最后执行,因此在您的场景中处理程序将在Pause the execution for cq5 to come up 任务之后执行 .

    【讨论】:

    • 如果我在同一个 playbook 中为不同任务定义了相同的处理程序,如果每个任务都有一些变化,处理程序会只执行一次还是针对每个任务执行?
    • 是的,例如,如果您使用 10 个不同的任务更改 10 个配置并通知所有任务的相同处理程序,则处理程序将只执行一次。
    • 有趣!那么如果我想在剧本执行结束之前调用这个处理程序,我该怎么办。即我希望我的服务在 task1 和 task2 之后启动。还有任务 3 和任务 4,它们不需要处理程序即可运行。
    • 似乎是一个不同的问题。请单独发布 MCVE 和预期行为。 Ans 请彻底检查docs.ansible.com/ansible/latest/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多