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
you have no action in that task, guessing you want:
- name: Host Count
debug:
msg: "{{ groups['ios_switches'] | length }}"
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
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