# issue in counting no. of host in inventory ?

**URL:** <https://forum.ansible.com/t/issue-in-counting-no-of-host-in-inventory/24498>\
**Category:** Ansible Project\
**Created:** [May 16, 2017, 6:23pm UTC](https://forum.ansible.com/t/issue-in-counting-no-of-host-in-inventory/24498 "2017-05-16T18:23:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurminder\_Singh](https://avatars.discourse-cdn.com/v4/letter/g/b5a626/32.png) [@Gurminder\_Singh](https://forum.ansible.com/u/Gurminder_Singh)\
**Post date:** [May 16, 2017, 6:23pm UTC](https://forum.ansible.com/t/issue-in-counting-no-of-host-in-inventory/24498/1 "2017-05-16T18:23:57Z")

</div>

I wrote a playbook to count number of host in inventory, can someone look or suggest better way of achieving this task, this play gives error in compling stage itself, however when I check the syntax in [http://codebeautify.org/yaml-validator](http://codebeautify.org/yaml-validator) this shows it is valid.

Please add your comment and suggestions ?

**My Playbook**

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 16, 2017, 9:16pm UTC](https://forum.ansible.com/t/issue-in-counting-no-of-host-in-inventory/24498/2 "2017-05-16T21:16:25Z")

</div>

you have no action in that task, guessing you want:

- name: Host Count  
&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ groups['ios\_switches'] | length }}"

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 17, 2017, 5:30pm UTC](https://forum.ansible.com/t/issue-in-counting-no-of-host-in-inventory/24498/3 "2017-05-17T17:30:30Z")

</div>

It tasks and not task.

---

<div class="post-metadata">

**Author:** ![Gurminder\_Singh](https://avatars.discourse-cdn.com/v4/letter/g/b5a626/32.png) [@Gurminder\_Singh](https://forum.ansible.com/u/Gurminder_Singh)\
**Post date:** [May 18, 2017, 7:57pm UTC](https://forum.ansible.com/t/issue-in-counting-no-of-host-in-inventory/24498/4 "2017-05-18T19:57:15Z")

</div>

Thank You Kai,

I corrected that and made some more changes to my playbook which eventually worked. We can also use

debug:  
msg: “Host count: {{ ansible\_play\_hosts | length }}”

- _ansible\_play\_hosts_ is an Ansible-defined variable which is a list of the hosts in the current play, i.e. what the above pattern matches

- **run\_once** ensures we don’t run the debug command for every single host in the list, which would be a big waste of time

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 18, 2017, 8:08pm UTC](https://forum.ansible.com/t/issue-in-counting-no-of-host-in-inventory/24498/5 "2017-05-18T20:08:06Z")

</div>

or just run on localhost

- name: Host Count In Inventory  
&nbsp;&nbsp;hosts: localhost  
&nbsp;&nbsp;gather\_facts: no  
&nbsp;&nbsp;tasks:

&nbsp;&nbsp;&nbsp;&nbsp;- name: Host Count  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "Host Count: {{ groups['ios'] | length }}"

alternatively:

ansible ios --list-hosts|head -n 1
