issue in counting no. of host in inventory ?

1,668 views
Skip to first unread message

Gurminder Singh

unread,
May 16, 2017, 2:23:58 PM5/16/17
to Ansible Project
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 this shows it is valid.

Please add your comment and suggestions ?

My Playbook
---
- name: Host Entry in Inventory
  hosts: "ios_switches"
  gather_facts: no
  connection: local

  task:
  - name: Host Count
       vars:
          num_hosts: "{{ groups['ios_switches'] | length }}"

Error I get after running playbook

ERROR! 'task' is not a valid attribute for a Play

The error appears to have been in '/home/gurmisin/ansible/host_count.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- name: Host Entry in Inventory
  ^ here

Brian Coca

unread,
May 16, 2017, 5:16:39 PM5/16/17
to Ansible Project
you have no action in that task, guessing you want:

- name: Host Count
debug:
msg: "{{ groups['ios_switches'] | length }}"


----------
Brian Coca

Kai Stian Olstad

unread,
May 17, 2017, 1:30:51 PM5/17/17
to ansible...@googlegroups.com
On 16. mai 2017 20:23, Gurminder Singh wrote:
> 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 this shows it is valid.
>
> Please add your comment and suggestions ?
>
> *My Playbook*
> ---
> - name: Host Entry in Inventory
> hosts: "ios_switches"
> gather_facts: no
> connection: local
>
> task:
> - name: Host Count
> vars:
> num_hosts: "{{ groups['ios_switches'] | length }}"
>
> *Error I get after running playbook*
>
> ERROR! 'task' is not a valid attribute for a Play

It tasks and not task.

--
Kai Stian Olstad

Gurminder Singh

unread,
May 18, 2017, 3:57:15 PM5/18/17
to Ansible Project, ansible-pr...@olstad.com
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


---
- name: Host Count In Inventory
  hosts: ios
  gather_facts: no
  connection: local

  tasks:

    - name: Host Count
      run_once: true
      debug:
         msg: "Host Count: {{ groups['ios'] | length }}"

Brian Coca

unread,
May 18, 2017, 4:08:13 PM5/18/17
to Ansible Project, ansible-pr...@olstad.com
or just run on localhost

- name: Host Count In Inventory
hosts: localhost
gather_facts: no
tasks:

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


alternatively:

ansible ios --list-hosts|head -n 1
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages