1

I am trying to automate some tasks using Ansible. In my playbooks, I have a copy task and then i change the permissions of the file. I need the service to restart after this task. I am including notify and have also declared my handler, but strangely this handler is never getting invoked.

Excerpt from my playbook

- 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

And here is my handler file contents.

---

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

The o/p after running the playbook

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!"}

But when i run this playbook, the restart of this service is not invoked. Why is this so?

Can we not use notify in file modules? Any help will be deeply appreciated.

Gaurav Parashar
  • 1,347
  • 2
  • 19
  • 21

1 Answers1

5

You can attach notify to any module.

But Ansible will notify handler only when task is in changed state – this is on purpose to prevent unnecessary handlers execution (e.g. service restarts) on subsequent playbook runs.

Your log excerpts show "changed": false for the task in question, so handler execution is not triggered.

Also keep in mind that handlers are executed at the very end of the role/playbook unless they are explicitly flushed with meta, so in your scenario handler will be executed after Pause the execution for cq5 to come up task.

Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • If i have the same handlers defined for different tasks in the same playbook, will the handler get executed just once or for each tasks, provided there was some changes in each of the tasks? – Gaurav Parashar Jan 22 '18 at 14:22
  • yes, if for example you change 10 configs with 10 different tasks and notify same handler from all of them, the handler will be executed only once. – Konstantin Suvorov Jan 22 '18 at 14:24
  • Interesting! So what do i do if i want to invoke this handler before the end of playbook execution. i.e. I want my service to be started after task1 and also after task2. And there are say tasks3 and tasks4 as well, which don't require a handler to run. – Gaurav Parashar Jan 22 '18 at 14:27
  • Seems like a different question. Please make separate post with MCVE and expected behaviour. Ans please thoroughly inspect http://docs.ansible.com/ansible/latest/playbooks_intro.html#handlers-running-operations-on-change – Konstantin Suvorov Jan 22 '18 at 14:31