# Determine last host in host list

**URL:** <https://forum.ansible.com/t/determine-last-host-in-host-list/34463>\
**Category:** Ansible Project\
**Created:** [January 18, 2021, 7:32pm UTC](https://forum.ansible.com/t/determine-last-host-in-host-list/34463 "2021-01-18T19:32:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark.tovey](https://avatars.discourse-cdn.com/v4/letter/m/ce73a5/32.png) [@mark.tovey](https://forum.ansible.com/u/mark.tovey)\
**Post date:** [January 18, 2021, 7:32pm UTC](https://forum.ansible.com/t/determine-last-host-in-host-list/34463/1 "2021-01-18T19:32:05Z")

</div>

I need to determine how many hosts are left to be worked on in a playbook run. I have set serial to 1 in the playbook so only one host will be worked on at a time, and I have a long pause at the bottom of the playbook to force a settling period between hosts. But if the current host is the last host in the list, there is no need to pause; the playbook can just end immediately. So, how can I determine if there are no more hosts after the current host?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 18, 2021, 9:11pm UTC](https://forum.ansible.com/t/determine-last-host-in-host-list/34463/2 "2021-01-18T21:11:35Z")

</div>

Test the \*last batch\*. For example

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Last batch  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_play\_hosts\_all[-1] == ansible\_play\_batch[-1]

See "Special variables"  
[https://docs.ansible.com/ansible/latest/reference\_appendices/special\_variables.html](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html)

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 18, 2021, 10:12pm UTC](https://forum.ansible.com/t/determine-last-host-in-host-list/34463/3 "2021-01-18T22:12:50Z")

</div>

The next option is

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: ansible\_play\_hosts\_all[-1] in ansible\_play\_batch

But, this won't work if the order is changed. See "Ordering execution  
based on inventory"  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_strategies.html#ordering-execution-based-on-inventory](https://docs.ansible.com/ansible/latest/user_guide/playbooks_strategies.html#ordering-execution-based-on-inventory)

Then, the next option is

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hleft: "{{ hostvars[ansible\_play\_hosts\_all.0]['hleft']|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default(ansible\_play\_hosts\_all)|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;difference(ansible\_play\_batch) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: "{{ ansible\_play\_hosts\_all.0 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_facts: true  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Last batch  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: not hostvars[ansible\_play\_hosts\_all.0]['hleft']

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [January 18, 2021, 11:17pm UTC](https://forum.ansible.com/t/determine-last-host-in-host-list/34463/4 "2021-01-18T23:17:32Z")

</div>

That feels overly fragile and difficult to read.

I would just set a flag variable and check whether there are any hosts that don’t have it set:

tasks:

- set\_fact:  
play\_is\_done: true

- debug:  
msg: I’m not last  
when: ansible\_play\_hosts | map(‘extract’, hostvars, ‘play\_is\_done’) | reject(‘defined’) | list

---

<div class="post-metadata">

**Author:** ![mark.tovey](https://avatars.discourse-cdn.com/v4/letter/m/ce73a5/32.png) [@mark.tovey](https://forum.ansible.com/u/mark.tovey)\
**Post date:** [January 19, 2021, 3:43pm UTC](https://forum.ansible.com/t/determine-last-host-in-host-list/34463/5 "2021-01-19T15:43:52Z")

</div>

Some good answers! Thanks!-Mark

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 19, 2021, 5:53pm UTC](https://forum.ansible.com/t/determine-last-host-in-host-list/34463/6 "2021-01-19T17:53:31Z")

</div>

Consider that you have parallel execution so 'last host' might not be  
the last one executing (unless serial=1 or forks=1).
