Q:

Ansible playbook skip task on last node in serial execution

I want to run some ansible task on 4 servers one by one, i.e. on serial manner. But there will be a pause in between. So, I have added the pause at last in the playbook, but I want it to be skipped on last server. Otherwise it will wait for no reason. Please let me know how to implement this.
---
- hosts: server1,server2,server3,server4
  serial: 1
  vars_files:
    - ./vars.yml

  tasks:
    - name: Variable test
      pause:
        minutes: 1

A:

Really interesting problem which forced me to look for an actual solution. Here is the quickest one I came up with.
The ansible special variables documentation defines the ansible_play_hosts_all variable as follow

List of all the hosts that were targeted by the play

The list of hosts in that var is in the order it was found inside the inventory.
Provided you use the default inventory order for your play, you can set a test that will trigger the task unless the current host is the last one in that list:
when: inventory_hostname != ansible_play_hosts_all[-1]

As reported by @Vladimir in the comments below, if you change the play order parameter from default, this approach will break.

