Ansible – Handlers Failing When Using include_tasks

ansibleansible-playbook

This ansible script works.

---
- name: Create Swap
  hosts: serverName
  gather_facts: yes
  become: true
  become_user: root
  become_method: sudo

  tasks:
    - name: Create swap
      shell:
        'dd if=/dev/zero of=/tmp/swapfile bs=1024 count=563200 && mkswap /tmp/swapfile && swapon /tmp/swapfile && swapon -s'      
      when: 
        ansible_facts['memory_mb']['swap']['total'] == 0
      notify: Disable swap

    - name: Collect selected facts
      setup:

  handlers:
    - name: Disable swap
      shell:
        'swapoff /tmp/swapfile && rm -rf /tmp/swapfile'
      when: 
        ansible_facts['memory_mb']['swap']['total'] > 500

But when I break the script in multiple unit.

tree 
.
├── main.yml
├── roles
│   └── swap_creater_560mb.yml

main.yml ==>

---
- name: Create Swap
  hosts: serverName
  gather_facts: yes
  become: true
  become_user: root
  become_method: sudo

  tasks:
    - name: Create swap
      include_tasks: roles/swap_creater_560mb.yml

And swap_creater_560mb.yml ==>

---
    - name: Create swap
      shell:
        'dd if=/dev/zero of=/tmp/swapfile bs=1024 count=563200 && mkswap /tmp/swapfile && swapon /tmp/swapfile && swapon -s'      
      when: 
        ansible_facts['memory_mb']['swap']['total'] == 0
      notify: Disable swap

    - name: Collect selected facts
      setup:

  handlers:
    - name: Disable swap
      shell:
        'swapoff /tmp/swapfile && rm -rf /tmp/swapfile'
      when: 
        ansible_facts['memory_mb']['swap']['total'] > 500

Whenever I create a swap I want to notify handler Disable swap. But when I include_tasks:roles/swap_creater_560mb.yml I get below error.

TASK [Create swap] *********************************************************************************************************fatal: [serverName]: FAILED! => {"reason": "We were unable to read either as JSON nor YAML, these are the errors we got from each:\nJSON: No JSON object could be decoded\n\nSyntax Error while loading YAML.\n did not find expected \n\nThe error appears to be in '/home/USER/testing_scripts/swap_creator/roles/swap_creater_560mb.yml': line 15, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n handlers:\n ^ here\n"}

Can anyone help me or direct me to what's I am doing wrong? I am new to ansible, I might be missing something.

Best Answer

handlers can only be set in a playbook (or in the handlers directory of a role). It cannot be set in a task list included with include_tasks. If you want to factor out those tasks and handlers into seperate files, create an actual ansible role.

The layout would look like:

main.yml
roles/
  swap_creater_560mb/
    tasks/
      main.yml
    handlers/
      main.yml

Where tasks/main.yml has:

- name: Create swap
  shell:
    'dd if=/dev/zero of=/tmp/swapfile bs=1024 count=563200 && mkswap /tmp/swapfile && swapon /tmp/swapfile && swapon -s'      
  when: 
    ansible_facts['memory_mb']['swap']['total'] == 0
  notify: Disable swap

- name: Collect selected facts
  setup:

And handlers/main.yml has:

- name: Disable swap
  shell:
    'swapoff /tmp/swapfile && rm -rf /tmp/swapfile'
  when: 
    ansible_facts['memory_mb']['swap']['total'] > 500

And then in your main playbook:

- name: Create Swap
  hosts: serverName
  gather_facts: yes
  become: true
  become_user: root
  become_method: sudo

  tasks:
    - name: Create swap
      include_role:
        name: swap_creater_560mb